JAVA dfs with static sum


  • 0
    T
     int sum;
        public TreeNode convertBST(TreeNode root) {
            dfs (root);
            return root;
        }
        private void dfs(TreeNode node) {
            if (node == null) {
                return;
            }
            dfs(node.right);
            sum += node.val;
            node.val = sum;
            dfs(node.left);
        }
    

Log in to reply
 

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