Java iterative reverse inorder traversal


  • 0
    U
    public TreeNode convertBST(TreeNode root) {
        
        TreeNode rootNode = root;
        int sum = 0;
        Stack<TreeNode> stack = new Stack<>();
    
        while (root != null || !stack.isEmpty()) {
            while(root != null) {
                stack.push(root);
                root = root.right;
            }
            TreeNode temp = stack.pop();
            sum += temp.val;
            temp.val = sum;
            root = temp.left;
        }
        
        return rootNode; 
    }

Log in to reply
 

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