Day 23: BST Level-Order Traversal

  • + 0 comments

    JavaSCript

    this.levelOrder = function(root) {
        if (!root) {
            return;
        }
    
        const queue = [];
        queue.push(root);
    
        while (queue.length > 0) {
            const node = queue.shift();
            process.stdout.write(node.data + " ");
    
            if (node.left) {
                queue.push(node.left);
            }
            if (node.right) {
                queue.push(node.right);
            }
        }
    };