leetcode 538. 把二叉搜索树转换为累加树 【时间击败100.00%】 【内存击败96.46%】
因为二叉搜索树的左子树<根<右子树的性质,按right-root-left的顺序遍历很容易求出累加和
1 int add = 0; 2 3 public TreeNode convertBST(TreeNode root) { 4 if (root == null) return root; 5 convertBST(root.right); 6 root.val += add; 7 add = root.val; 8 convertBST(root.left); 9 return root; 10 }

浙公网安备 33010602011771号