Iteration solution


  • 0
    L
    function inorderSuccessor(root, p) {
        var stack = [];
        var curr = root;
        var foundP = false;
        
        while (curr || stack.length) {
            while (curr) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            if (foundP) {
                return curr;
            }
            
            if (curr === p) {
                foundP = true;
            }
            
            curr = curr.right;
        }
        
        return null;
    }

Log in to reply
 

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