Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dbe51e8

Browse filesBrowse files
committed
add 1028
1 parent 867dc31 commit dbe51e8
Copy full SHA for dbe51e8

File tree

132 files changed

+447
-139
lines changed
Filter options

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Dismiss banner

132 files changed

+447
-139
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+136-137Lines changed: 136 additions & 137 deletions
File renamed without changes.
File renamed without changes.

‎note/0209/README.md

Copy file name to clipboard
+81Lines changed: 81 additions & 0 deletions

‎note/1014/README.md

Copy file name to clipboardExpand all lines: note/1014/README.md
+2-2Lines changed: 2 additions & 2 deletions

‎note/1028/README.md

Copy file name to clipboard
+148Lines changed: 148 additions & 0 deletions
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.blankj.hard._1028;
2+
3+
import com.blankj.structure.TreeNode;
4+
5+
import java.util.LinkedList;
6+
7+
/**
8+
* <pre>
9+
* author: Blankj
10+
* blog : http://blankj.com
11+
* time : 2020/06/19
12+
* desc :
13+
* </pre>
14+
*/
15+
public class Solution {
16+
17+
public TreeNode recoverFromPreorder(String S) {
18+
char[] chars = S.toCharArray();
19+
int len = chars.length;
20+
LinkedList<TreeNode> stack = new LinkedList<>();
21+
for (int i = 0; i < len; ) {
22+
int level = 0, val = 0;
23+
while (chars[i] == '-') { // 获取所在层级,Character.isDigit() 会比较慢
24+
++i;
25+
++level;
26+
}
27+
while (i < len && chars[i] != '-') { // 获取节点的值
28+
val = val * 10 + chars[i++] - '0';
29+
}
30+
TreeNode curNode = new TreeNode(val);
31+
while (stack.size() > level) { // 栈顶不是父亲,栈顶出栈
32+
stack.removeLast();
33+
}
34+
if (level > 0) {
35+
TreeNode parent = stack.getLast();
36+
if (parent.left == null) { // 如果节点只有一个子节点,那么保证该子节点为左子节点。
37+
parent.left = curNode;
38+
} else {
39+
parent.right = curNode;
40+
}
41+
}
42+
stack.addLast(curNode);
43+
}
44+
return stack.get(0);
45+
}
46+
47+
// public TreeNode recoverFromPreorder(String S) {
48+
// char[] chars = S.toCharArray();
49+
// int len = chars.length;
50+
// List<TreeNode> levels = new LinkedList<>();
51+
// for (int i = 0; i < len; ) {
52+
// int level = 0, val = 0;
53+
// while (chars[i] == '-') { // 获取所在层级,Character.isDigit() 会比较慢
54+
// ++i;
55+
// ++level;
56+
// }
57+
// while (i < len && chars[i] != '-') { // 获取节点的值
58+
// val = val * 10 + chars[i++] - '0';
59+
// }
60+
// TreeNode curNode = new TreeNode(val);
61+
// if (level > 0) {
62+
// TreeNode parent = levels.get(level - 1);
63+
// if (parent.left == null) { // 如果节点只有一个子节点,那么保证该子节点为左子节点。
64+
// parent.left = curNode;
65+
// } else {
66+
// parent.right = curNode;
67+
// }
68+
// }
69+
// levels.add(level, curNode); // 因为是前序遍历(根-左-右),也就是右覆盖左时,此时左树已遍历完成,故无需考虑覆盖问题
70+
// }
71+
// return levels.get(0);
72+
// }
73+
74+
public static void main(String[] args) {
75+
Solution solution = new Solution();
76+
TreeNode.print(solution.recoverFromPreorder("1-2--3--4-5--6--7"));
77+
System.out.println("==============================================");
78+
TreeNode.print(solution.recoverFromPreorder("1-2--3---4-5--6---7"));
79+
}
80+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.