My code works well with my testcase,but not work after submit(javascript)


  • 0
    Q

    0_1508772876033_316ce148-cdd4-44dd-88d6-16437fd1dc50-image.png

    var maxDepth = function (root) {
    if(!root) return 0;
    let stack = [],
    cur = root;

            cur.depth = 1;
    
            while (cur || stack.length) {
                if (cur) {
                    visit(cur);
                    stack.push(cur);
                    cur = cur.left;
    
                } else {
                    cur = stack.pop();
                    cur = cur.right;
                }
            }
            return maxdepth;
        };
    
        function visit(cur) {
            const depth = cur.depth;
            if (depth > maxdepth) {
                maxdepth = depth;
            }
    
            if(cur.left){
                cur.left.depth = depth + 1;
            }
    
            if(cur.right){
                cur.right.depth = depth + 1;
            }
        }

Log in to reply
 

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