C# code with 7 lines,very simple DFS algorithms


  • 0
    M
        int sum = 0;
        public TreeNode ConvertBST(TreeNode root) {
              if (root == null) return null;               
              ConvertBST(root.right);
              root.val += sum;
              sum = root.val;
                ConvertBST(root.left);
                return root;
        }
    

Log in to reply
 

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