Non-recursive solution, use stack only


  • 0
    H
    var maxDepth = function(root) {
        var cur = [];
        var next = [];
        if (!root) return 0;
        
        cur.push(root);
        var depth = 0;
        
        while(cur.length) {
            while(cur.length) {
                var node = cur.pop();
                if (node.left) next.push(node.left);
                if (node.right) next.push(node.right);
            }
            cur = next;
            next = [];
            depth++;
        }
        return depth;
    };
    

Log in to reply
 

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