We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 47d087e commit d0fc9c1Copy full SHA for d0fc9c1
convert-BST-to-greater-tree.java
@@ -12,21 +12,19 @@
12
8 13
13
/ \ / \
14
5 9 12 15
15
+ It is reversed inorder traversal
16
*/
17
public class Solution {
- public void convertBST1(TreeNode root) {
18
+ int sum = 0;
19
+ public TreeNode convert(TreeNode root) {
20
if (root == null) return;
- TreeNode cur = root.right;
- while (cur != null) {
21
- convertBST1(cur);
22
- root.val += cur.val;
23
- cur = cur.left;
24
- }
25
-
+ convert(root.right);
+ sum += cur.val;
+ cur.val = sum;
+ convert(root.left);
26
}
27
public TreeNode convertBST(TreeNode root) {
28
- convertBST1(root);
29
- convertBST1(root.left);
+ convert(root);
30
return root;
31
32
0 commit comments