Day 23: BST Level-Order Traversal

  • + 0 comments

    in JS

    // Start of function levelOrder
        this.levelOrder = function(root) {
        if (!root) return;
    
            const queue = [];
            queue.push(root);
    
            while (queue.length > 0) { 
                const levelSize = queue.length; 
    
            for (let i = 0; i < levelSize; i++) { 
            const node = queue.shift(); 
            process.stdout.write(node.data + ' '); 
    
            if (node.left) queue.push(node.left); 
            if (node.right) queue.push(node.right);  
            }
            }  
        }; // End of function levelOrder