Skip to content

Commit 6324c1c

Browse files
committed
fd
1 parent 1816a9f commit 6324c1c

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@
375375
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Java](leetcode/solution/src/DiameterOfBinaryTree.java)|80|这题易错|
376376
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
377377
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
378+
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Java](leetcode/solution/src/ConstructStringFromBinaryTree.java)|90||
378379
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Java](leetcode/solution/src/MergeTwoBinaryTrees.java)|100|很简单|
379380
|637|[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description)| [Java](leetcode/solution/src/AverageOfLevelsInBinaryTree.java)|100||
380381
|652|[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Java](leetcode/solution/src/FindDuplicateSubtrees.java)|70|开始还没思路|
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/articles/construct-string-from-binary-tree/
3+
*/
4+
public class ConstructStringFromBinaryTree {
5+
6+
/**
7+
* 左子树如果为空'()'是不能省略的
8+
* 右子树如果为空可以省略
9+
*/
10+
public String tree2str(TreeNode t) {
11+
if (t == null) {
12+
return "";
13+
}
14+
if (t.left == null && t.right == null) {
15+
return t.val + "";
16+
}
17+
String left = "(" + tree2str(t.left) + ")";
18+
String right = t.right != null ? "(" + tree2str(t.right) + ")" : "";
19+
return t.val + left + right;
20+
}
21+
}

leetcode/src/Main.java

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

55
public class Main {
66

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-
}
7+
public String tree2str(TreeNode t) {
8+
if (t == null) {
9+
return "";
2210
}
23-
return root;
11+
if (t.left == null && t.right == null) {
12+
return t.val + "";
13+
}
14+
String left = "(" + tree2str(t.left) + ")";
15+
String right = t.right != null ? "(" + tree2str(t.right) + ")" : "";
16+
return t.val + left + right;
2417
}
2518

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

0 commit comments

Comments
 (0)