Javascript with two queue


  • 0
    B
    var minDepth = function(root) {
        if (!root)
            return 0;
    
        var level = 1;
        var queue = [root];
        
        while(true) {
            var nextQueue = [];
         
            while(queue.length > 0) {
                var node = queue.shift();
                
                if (node.left === null && node.right === null) {
                    return level;
                }else {
                    if (node.left)
                        nextQueue.push(node.left);
                        
                    if (node.right)
                        nextQueue.push(node.right);
               }
            }
           
           queue = nextQueue;
           level++
        }
        
        return level;
    };

Log in to reply
 

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