java code with stack by usting inoderTraverse


  • 0
    W
        Stack<TreeNode> stack = new Stack<>();
        TreeNode temp = root;
        int result = 0;
        while(!stack.isEmpty() || temp != null){
            while(temp != null){
                stack.push(temp);
                temp = temp.left;
            }
            temp = stack.pop();
            k--;
            if(k==0){
                result = temp.val;
                break;
            }
            temp = temp.right;
            
        }
        return result;
    }

  • 0
    S

    I think it's preOrder traverse.


Log in to reply
 

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