Skip to content

Commit 2a5adb5

Browse files
committed
add 538
1 parent d238ec1 commit 2a5adb5

File tree

8 files changed

+404
-1
lines changed

8 files changed

+404
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ Idx | Date | Question | Python| Java | Domain | Tag | Difficulty | Remark
3131
025|20200216|[2. Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)|[002p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/002_Add_Two_Numbers.ipynb)|[002j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/002/Solution.java)|List||Medium
3232
026|20200223|[530. Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)|[530p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/530_Minimum_Absolute_Difference_in_BST.ipynb)|[530j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/530/Solution.java)|BST|Recursive|Easy
3333
027|20200301|[938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/)|[938p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/938_Range_Sum_of_BST.ipynb)|[938j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/938/Solution.java)|BST|Recursive/Iterative|Easy
34-
028|20200301|[450. Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/)|[450p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/450_Delete_Node_in_a_BST.ipynb)|[450j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/450/Solution.java)|BST|Recursive|Medium
34+
028|20200301|[450. Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/)|[450p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/450_Delete_Node_in_a_BST.ipynb)|[450j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/450/Solution.java)|BST|Recursive|Medium
35+
029|20200329|[538. Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[538p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/538_Convert_BST_to_Greater_Tree.ipynb)|[538j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/538/Solution.java)|BST|Recursive/Iterative|Easy

images/538_Q.PNG

54.9 KB
Loading

images/538_S1.PNG

24.1 KB
Loading

images/538_S2.PNG

24.4 KB
Loading

images/538_S3.PNG

25 KB
Loading

ipynb_files/538_Convert_BST_to_Greater_Tree.ipynb

Lines changed: 352 additions & 0 deletions
Large diffs are not rendered by default.

java_codes/538/Solution.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Stack;
2+
3+
class TreeNode {
4+
int val;
5+
TreeNode left;
6+
TreeNode right;
7+
TreeNode(int x) {
8+
val = x;
9+
}
10+
}
11+
12+
// traverse
13+
class Solution {
14+
15+
int total = 0;
16+
public TreeNode convertBST(TreeNode root) {
17+
visit(root);
18+
return root;
19+
}
20+
21+
public void visit(TreeNode root) {
22+
if (root != null){
23+
visit(root.right);
24+
root.val += total;
25+
total = root.val;
26+
visit(root.left);
27+
}
28+
}
29+
}
30+
31+
// iterative
32+
class Solution {
33+
public TreeNode convertBST(TreeNode root) {
34+
int total = 0;
35+
TreeNode node = root;
36+
Stack<TreeNode> stack = new Stack<>();
37+
while (!stack.isEmpty() || node != null){
38+
while (node != null){
39+
stack.add(node);
40+
node = node.right;
41+
}
42+
node = stack.pop();
43+
total += node.val;
44+
node.val = total;
45+
node = node.left;
46+
}
47+
return root;
48+
}
49+
}

java_codes/java_codes.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<sourceFolder url="file://$MODULE_DIR$/530" isTestSource="false" />
3333
<sourceFolder url="file://$MODULE_DIR$/938" isTestSource="false" />
3434
<sourceFolder url="file://$MODULE_DIR$/450" isTestSource="false" />
35+
<sourceFolder url="file://$MODULE_DIR$/538" isTestSource="false" />
3536
</content>
3637
<orderEntry type="inheritedJdk" />
3738
<orderEntry type="sourceFolder" forTests="false" />

0 commit comments

Comments
 (0)