Javascript version with queue


  • 0
    T
    var levelOrder = function(root) {
        var ret = [];
        var pipe = [];
        if(root == null)
            return [];
        pipe.push(root);
        while(pipe.length > 0) {
            var list = [];
            var nodes = [];
            while(pipe.length > 0) {
                var t = pipe.shift();
                list.push(t.val);
                if(t.left != null) {
                    nodes.push(t.left);
                }
                if (t.right != null) {
                    nodes.push(t.right);
                }
            }
            ret.push(list.concat());
            for(var i = 0; i < nodes.length; i++) {
                pipe.push(nodes[i]);
            }
        }
        
        return ret;
    };
    
    

Log in to reply
 

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