Java AC pre-order solution


  • 0
    W
        private TreeNode prev;
        
        public void flatten(TreeNode root) {
            prev = null;
            dfs(root);
        }
        
        private void dfs(TreeNode root) {
            if (root == null) return;
            TreeNode tmpRight = root.right;
            if (prev != null) {
                prev.right = root;
                prev.left = null;
            }
            prev = root;
            dfs(root.left);
            dfs(tmpRight);
        }
    }
    

Log in to reply
 

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