Simple JavaScript solution


  • 0
    J
    var levelOrder = function(root, total = []) {
        const results = [];
    
        function leveler(node, level) {
            if(!node) return;
            results[level] = results[level] || [];
            results[level].push(node.val);
            leveler(node.left, level + 1);
            leveler(node.right, level + 1);
        }
    
        leveler(root, 0)    
        return results;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.