Skip to content

Commit 1816a9f

Browse files
committed
fd
1 parent 67fa6a3 commit 1816a9f

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@
370370
|524|[Longest Word in Dictionary through Deleting](https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/)| [Java](leetcode/solution/src/LongestWordInDictionaryThroughDeleting.java)|||
371371
|526|[Beautiful Arrangement](https://leetcode.com/problems/beautiful-arrangement/)| [Java](leetcode/solution/src/BeautifulArrangement.java)|80||
372372
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)| [Java](leetcode/solution/src/EncodeAndDecodeTinyURL.java)|100||
373+
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Java](leetcode/solution/src/ConvertBSTToGreaterTree.java)|100||
373374
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Java](leetcode/solution/src/ReverseStringII.java)|100||
374375
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Java](leetcode/solution/src/DiameterOfBinaryTree.java)|80|这题易错|
375376
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* https://leetcode.com/articles/convert-bst-to-greater-tree/
5+
*/
6+
public class ConvertBSTToGreaterTree {
7+
8+
public TreeNode convertBST(TreeNode root) {
9+
Stack<TreeNode> stack = new Stack<>();
10+
int sum = 0;
11+
TreeNode node = root;
12+
while (!stack.isEmpty() || node != null) {
13+
if (node != null) {
14+
stack.push(node);
15+
node = node.right;
16+
} else {
17+
node = stack.pop();
18+
int s = sum;
19+
sum += node.val;
20+
node.val += s;
21+
node = node.left;
22+
}
23+
}
24+
return root;
25+
}
26+
}

leetcode/src/Main.java

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,23 @@
44

55
public class Main {
66

7-
public int pathSum(TreeNode root, int sum) {
8-
HashMap<Integer, Integer> map = new HashMap<>();
9-
map.put(0, 1);
10-
11-
int[] count = new int[1];
12-
helper(root, sum, 0, map, count);
13-
return count[0];
14-
}
15-
16-
private void helper(TreeNode node, int target, int sum, HashMap<Integer, Integer> map, int[] count) {
17-
if (node == null) {
18-
return;
19-
}
20-
21-
sum += node.val;
22-
if (map.containsKey(sum - target)) {
23-
count[0] += map.get(sum - target);
7+
public TreeNode convertBST(TreeNode root) {
8+
Stack<TreeNode> stack = new Stack<>();
9+
int sum = 0;
10+
TreeNode node = root;
11+
while (!stack.isEmpty() || node != null) {
12+
if (node != null) {
13+
stack.push(node);
14+
node = node.right;
15+
} else {
16+
node = stack.pop();
17+
int s = sum;
18+
sum += node.val;
19+
node.val += s;
20+
node = node.left;
21+
}
2422
}
25-
26-
map.put(sum, map.getOrDefault(sum, 0) + 1);
27-
28-
helper(node.left, target, sum, map, count);
29-
helper(node.right, target, sum, map, count);
30-
31-
map.put(sum, map.getOrDefault(sum, 0) - 1);
23+
return root;
3224
}
3325

3426
public static void main(String[] args) {

0 commit comments

Comments
 (0)