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 98a66d2 commit 47d087eCopy full SHA for 47d087e
convert-BST-to-greater-tree.java
@@ -14,15 +14,19 @@
14
5 9 12 15
15
*/
16
public class Solution {
17
- public void convertBSTtoGT(TreeNode root, int passedDown) {
+ public void convertBST1(TreeNode root) {
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);
+ TreeNode cur = root.right;
+ while (cur != null) {
+ convertBST1(cur);
+ root.val += cur.val;
23
+ cur = cur.left;
24
+ }
25
+
26
}
27
public TreeNode convertBST(TreeNode root) {
- convertBSTtoGT(root);
28
+ convertBST1(root);
29
+ convertBST1(root.left);
30
return root;
31
32
0 commit comments