Skip to content

Commit 98a66d2

Browse files
Create convert-BST-to-greater-tree.java
1 parent 842edbd commit 98a66d2

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

convert-BST-to-greater-tree.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
10+
10
11+
/ \
12+
8 13
13+
/ \ / \
14+
5 9 12 15
15+
*/
16+
public class Solution {
17+
public void convertBSTtoGT(TreeNode root, int passedDown) {
18+
if (root == null) return;
19+
convertBSTtoGT(root.right, 0);
20+
root.val = root.val + passedDown;
21+
if (root.right != null) root.val += root.right.val;
22+
convertBSTtoGT(root.left, root.val);
23+
}
24+
public TreeNode convertBST(TreeNode root) {
25+
convertBSTtoGT(root);
26+
return root;
27+
}
28+
}

0 commit comments

Comments
 (0)