Skip to content

Commit 247448d

Browse files
committed
101
1 parent 20fa338 commit 247448d

File tree

13 files changed

+855
-3
lines changed

13 files changed

+855
-3
lines changed

SUMMARY.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,6 @@
100100
* [97. Interleaving String](leetCode-97-Interleaving-String.md)
101101
* [98. Validate Binary Search Tree](leetCode-98-Validate-Binary-Search-Tree.md)
102102
* [99. Recover Binary Search Tree](leetcode-99-Recover-Binary-Search-Tree.md)
103-
* [100. Same Tree](leetcode-100-Same-Tree.md)
103+
* [100. Same Tree](leetcode-100-Same-Tree.md)
104+
* [101 题到 101 题](leetcode-101-200.md)
105+
* [101. Symmetric Tree](leetcode-101-Symmetric-Tree.md)

book.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"summary": "SUMMARY.md"
1515

1616
},
17-
"plugins": ["katex","splitter","anchor-navigation-ex","github-buttons","copy-code-button","-lunr", "-search", "search-plus","ad","-livereload","meta","sitemap"],
17+
"plugins": ["katex","splitter","anchor-navigation-ex","github-buttons","copy-code-button","-lunr", "-search", "search-plus","ad","-livereload","meta","sitemap","toggle-chapters"],
1818
"pluginsConfig": {
1919
"github-buttons": {
2020
"buttons": [{

leetCode-94-Binary-Tree-Inorder-Traversal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ cur 指向 null,结束遍历。
174174
**2.2** last.right 不为 null,说明之前已经访问过,第二次来到这里,表明当前子树遍历完成,保存 cur 的值,更新 cur = cur.right
175175

176176
```java
177-
public List<Integer> inorderTraversal3(TreeNode root) {
177+
public List<Integer> inorderTraversal(TreeNode root) {
178178
List<Integer> ans = new ArrayList<>();
179179
TreeNode cur = root;
180180
while (cur != null) {

leetcode-101-200.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# leetcode 101 到 200 题
2+
3+
[101. Symmetric Tree](<https://leetcode.wang/leetcode-101-Symmetric-Tree.html>)
4+

leetcode-101-Symmetric-Tree.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# 题目描述(简单难度)
2+
3+
![](https://windliang.oss-cn-beijing.aliyuncs.com/101.jpg)
4+
5+
判断一个二叉树是否关于中心轴对称。
6+
7+
# 解法一
8+
9+
[100 题](<https://leetcode.wang/leetcode-100-Same-Tree.html>) 判断两个二叉树是否相等其实是一样的思路,都是用某种遍历方法来同时遍历**两个树**,然后看是否**对应相等**
10+
11+
这里的需要遍历的两个树就是左子树和右子树了。
12+
13+
这里的对应相等的话,因为判断左子树 A 和右子树 B 是否对称,需要判断两点。
14+
15+
* A 的根节点和 B 的根节点是否相等
16+
* A 的左子树和 B 的右子树是否相等,同时 A 的右子树和左子树是否相等。
17+
18+
上边两点都满足,就表示是对称的。所以代码就出来了。
19+
20+
```java
21+
public boolean isSymmetric5(TreeNode root) {
22+
if (root == null) {
23+
return true;
24+
}
25+
return isSymmetricHelper(root.left, root.right);
26+
}
27+
28+
private boolean isSymmetricHelper(TreeNode left, TreeNode right) {
29+
//有且仅有一个为 null ,直接返回 false
30+
if (left == null && right != null || left != null && right == null) {
31+
return false;
32+
}
33+
if (left != null && right != null)
34+
//A 的根节点和 B 的根节点是否相等
35+
if (left.val != right.val) {
36+
return false;
37+
}
38+
//A 的左子树和 B 的右子树是否相等,同时 A 的右子树和左子树是否相等。
39+
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
40+
}
41+
//都为 null,返回 true
42+
return true;
43+
}
44+
```
45+
46+
# 解法二 DFS 栈
47+
48+
解法一其实就是类似于 DFS 的先序遍历。不同之处是对于 left 子树是正常的先序遍历 根节点 -> 左子树 -> 右子树 的顺序,对于 right 子树的话是 根节点 -> 右子树 -> 左子树 的顺序。
49+
50+
所以我们可以用栈,把递归改写为迭代的形式。
51+
52+
```java
53+
public boolean isSymmetric(TreeNode root) {
54+
if (root == null) {
55+
return true;
56+
}
57+
Stack<TreeNode> stackLeft = new Stack<>();
58+
Stack<TreeNode> stackRight = new Stack<>();
59+
TreeNode curLeft = root.left;
60+
TreeNode curRight = root.right;
61+
while (curLeft != null || !stackLeft.isEmpty() || curRight!=null || !stackRight.isEmpty()) {
62+
// 节点不为空一直压栈
63+
while (curLeft != null) {
64+
stackLeft.push(curLeft);
65+
curLeft = curLeft.left; // 考虑左子树
66+
}
67+
while (curRight != null) {
68+
stackRight.push(curRight);
69+
curRight = curRight.right; // 考虑右子树
70+
}
71+
//长度不同就返回 false
72+
if (stackLeft.size() != stackRight.size()) {
73+
return false;
74+
}
75+
// 节点为空,就出栈
76+
curLeft = stackLeft.pop();
77+
curRight = stackRight.pop();
78+
79+
// 当前值判断
80+
if (curLeft.val != curRight.val) {
81+
return false;
82+
}
83+
// 考虑右子树
84+
curLeft = curLeft.right;
85+
curRight = curRight.left;
86+
}
87+
return true;
88+
}
89+
```
90+
91+
当然我们也可以使用中序遍历或者后序遍历,是一样的道理。
92+
93+
# 解法三 BFS 队列
94+
95+
DFS 考虑完了,当然还有 BFS,一层一层的遍历两个树,然后判断**对应**的节点是否相等即可。
96+
97+
利用两个队列来保存下一次遍历的节点即可。
98+
99+
```java
100+
public boolean isSymmetric6(TreeNode root) {
101+
if (root == null) {
102+
return true;
103+
}
104+
Queue<TreeNode> leftTree = new LinkedList<>();
105+
Queue<TreeNode> rightTree = new LinkedList<>();
106+
//两个树的根节点分别加入
107+
leftTree.offer(root.left);
108+
rightTree.offer(root.right);
109+
while (!leftTree.isEmpty() && !rightTree.isEmpty()) {
110+
TreeNode curLeft = leftTree.poll();
111+
TreeNode curRight = rightTree.poll();
112+
if (curLeft == null && curRight != null || curLeft != null && curRight == null) {
113+
return false;
114+
}
115+
if (curLeft != null && curRight != null) {
116+
if (curLeft.val != curRight.val) {
117+
return false;
118+
}
119+
//先加入左子树后加入右子树
120+
leftTree.offer(curLeft.left);
121+
leftTree.offer(curLeft.right);
122+
123+
//先加入右子树后加入左子树
124+
rightTree.offer(curRight.right);
125+
rightTree.offer(curRight.left);
126+
}
127+
128+
}
129+
if (!leftTree.isEmpty() || !rightTree.isEmpty()) {
130+
return false;
131+
}
132+
return true;
133+
}
134+
```
135+
136+
#
137+
138+
总体上来说和 [100 题](<https://leetcode.wang/leetcode-100-Same-Tree.html>) 是一样的,只不过这里的两棵树对应相等,是左对右,右对左。

node_modules/gitbook-plugin-toggle-chapters/.npmignore

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)