Skip to content

Commit 24c34fb

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 538_Convert_BST_to_Greater_Tree.java
1 parent 8ee5aae commit 24c34fb

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public TreeNode convertBST(TreeNode root) {
3+
dfs(root, 0);
4+
return root;
5+
}
6+
7+
private int dfs(TreeNode root, int value) {
8+
if (root == null) {
9+
return value;
10+
}
11+
12+
int right = dfs(root.right, value);
13+
root.val += right;
14+
int left = dfs(root.left, root.val);
15+
return left;
16+
}
17+
}

0 commit comments

Comments
 (0)