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 842edbd commit 98a66d2Copy full SHA for 98a66d2
convert-BST-to-greater-tree.java
@@ -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