Categories
Algorithms Javascript

BST Iterative Preorder Traversal in JS

function preorder(root) {
	const stack = [];
	while (stack.length || root) {
		while (root) {
			stack.push(root);
			root = root.left;
		}
		root = stack.pop();
		console.log(root.val);
		root = root.right;
	}
};

Leave a Reply

Your email address will not be published. Required fields are marked *