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

Sign up
Appearance settings

Commit 4dedeff

Browse files
committed
commit solution 101
1 parent 23717ec commit 4dedeff

File tree

6 files changed

+152
-5
lines changed

6 files changed

+152
-5
lines changed

‎index-tags.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
141141
| --- | --- | --- | --- | --- |
142142
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
143+
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
143144

144145
#### **递归**
145146

@@ -148,9 +149,14 @@
148149
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
149150
| --- | --- | --- | --- | --- |
150151
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
152+
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
151153

152154
#### **广度优先搜索**
153155

156+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
157+
| --- | --- | --- | --- | --- |
158+
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
159+
154160
#### **贪心算法**
155161

156162
#### **二叉搜索树**

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
| [83](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list) | [删除排序链表中的重复元素](/solution/1-99/0083.remove-duplicates-from-sorted-list/) | `链表` | <font color=green>简单</font> ||
2929
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3030
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
31-
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
31+
| [101](https://leetcode-cn.com/problems/symmetric-tree) | [对称二叉树](/solution/100-199/0101.symmetric-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> ||
3232
| [104](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree) | [二叉树的最大深度](/solution/100-199/0104.maximum-depth-of-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3333
| [107](https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii) | [二叉树的层次遍历 ii](/solution/100-199/0107.binary-tree-level-order-traversal-ii/) | ``,`广度优先搜索` | <font color=green>简单</font> |
3434
| [108](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree) | [将有序数组转换为二叉搜索树](/solution/100-199/0108.convert-sorted-array-to-binary-search-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,70 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [101. 对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/description/)
22

33
### 题目描述
44

5+
<p>给定一个二叉树,检查它是否是镜像对称的。</p>
6+
7+
<p>&nbsp;</p>
8+
9+
<p>例如,二叉树&nbsp;<code>[1,2,2,3,4,4,3]</code> 是对称的。</p>
10+
11+
<pre> 1
12+
/ \
13+
2 2
14+
/ \ / \
15+
3 4 4 3
16+
</pre>
17+
18+
<p>&nbsp;</p>
19+
20+
<p>但是下面这个&nbsp;<code>[1,2,2,null,3,null,3]</code> 则不是镜像对称的:</p>
21+
22+
<pre> 1
23+
/ \
24+
2 2
25+
\ \
26+
3 3
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong>进阶:</strong></p>
32+
33+
<p>你可以运用递归和迭代两种方法解决这个问题吗?</p>
534

635
### 解题思路
736

37+
1. 递归解法
838

939
### 具体解法
1040

11-
<!-- tabs:start -->
1241

1342
#### **Golang**
1443
```go
44+
type TreeNode struct {
45+
Val int
46+
Left *TreeNode
47+
Right *TreeNode
48+
}
1549

50+
func isSymmetric(root *TreeNode) bool {
51+
if root == nil {
52+
return true
53+
}
54+
return recursive(root.Left, root.Right)
55+
}
56+
func recursive(p, q *TreeNode) bool {
57+
if p == nil && q == nil {
58+
return true
59+
}
60+
if p == nil && q != nil {
61+
return false
62+
}
63+
if p != nil && q == nil {
64+
return false
65+
}
66+
return p.Val == q.Val && recursive(p.Left, q.Right) && recursive(p.Right, q.Left)
67+
}
1668
```
1769

18-
<!-- tabs:end -->
1970

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=101 lang=golang
5+
*
6+
* [101] 对称二叉树
7+
*/
8+
9+
// @lc code=start
10+
/**
11+
* Definition for a binary tree node.
12+
* type TreeNode struct {
13+
* Val int
14+
* Left *TreeNode
15+
* Right *TreeNode
16+
* }
17+
*/
18+
19+
type TreeNode struct {
20+
Val int
21+
Left *TreeNode
22+
Right *TreeNode
23+
}
24+
25+
func isSymmetric(root *TreeNode) bool {
26+
if root == nil {
27+
return true
28+
}
29+
return recursive(root.Left, root.Right)
30+
}
31+
func recursive(p, q *TreeNode) bool {
32+
if p == nil && q == nil {
33+
return true
34+
}
35+
if p == nil && q != nil {
36+
return false
37+
}
38+
if p != nil && q == nil {
39+
return false
40+
}
41+
return p.Val == q.Val && recursive(p.Left, q.Right) && recursive(p.Right, q.Left)
42+
}
43+
44+
// @lc code=end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsSymmetric(t *testing.T) {
8+
var ret bool
9+
p := &TreeNode{
10+
Val: 2,
11+
Left: &TreeNode{
12+
Val: 3,
13+
Left: &TreeNode{
14+
Val: 4,
15+
Left: nil,
16+
Right: nil,
17+
},
18+
Right: &TreeNode{
19+
Val: 5,
20+
Left: nil,
21+
Right: nil,
22+
},
23+
},
24+
Right: &TreeNode{
25+
Val: 3,
26+
Left: nil,
27+
Right: &TreeNode{
28+
Val: 4,
29+
Left: nil,
30+
Right: nil,
31+
},
32+
},
33+
}
34+
35+
ret = false
36+
if ret != isSymmetric(p) {
37+
t.Fatalf("case fails %v\n", ret)
38+
}
39+
40+
p1 := &TreeNode{}
41+
ret = true
42+
if ret != isSymmetric(p1) {
43+
t.Fatalf("case fails %v\n", ret)
44+
}
45+
46+
}

‎solution/100-199/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- 📚&nbsp;&nbsp;题目列表
88
- [100 - 199题](index-tags.md)
99
- [100. 相同的树 ✅](solution/100-199/0100.same-tree/)
10-
- [101. 对称二叉树](solution/100-199/0101.symmetric-tree/)
10+
- [101. 对称二叉树](solution/100-199/0101.symmetric-tree/)
1111
- [102. 二叉树的层序遍历](solution/100-199/0102.binary-tree-level-order-traversal/)
1212
- [103. 二叉树的锯齿形层次遍历](solution/100-199/0103.binary-tree-zigzag-level-order-traversal/)
1313
- [104. 二叉树的最大深度](solution/100-199/0104.maximum-depth-of-binary-tree/)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /