Skip to content

Commit 47d087e

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

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

convert-BST-to-greater-tree.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414
5 9 12 15
1515
*/
1616
public class Solution {
17-
public void convertBSTtoGT(TreeNode root, int passedDown) {
17+
public void convertBST1(TreeNode root) {
1818
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);
19+
TreeNode cur = root.right;
20+
while (cur != null) {
21+
convertBST1(cur);
22+
root.val += cur.val;
23+
cur = cur.left;
24+
}
25+
2326
}
2427
public TreeNode convertBST(TreeNode root) {
25-
convertBSTtoGT(root);
28+
convertBST1(root);
29+
convertBST1(root.left);
2630
return root;
2731
}
2832
}

0 commit comments

Comments
 (0)