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 23717ec

Browse files
committed
commit solution 100
1 parent 51d0217 commit 23717ec

File tree

6 files changed

+170
-7
lines changed

6 files changed

+170
-7
lines changed

‎index-tags.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,18 @@
137137

138138
#### ****
139139

140+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
141+
| --- | --- | --- | --- | --- |
142+
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
143+
140144
#### **递归**
141145

142146
#### **深度优先搜素**
143147

148+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
149+
| --- | --- | --- | --- | --- |
150+
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
151+
144152
#### **广度优先搜索**
145153

146154
#### **贪心算法**

‎index-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| [70](https://leetcode-cn.com/problems/climbing-stairs) | [爬楼梯](/solution/1-99/0070.climbing-stairs/) | `动态规划` | <font color=green>简单</font> |
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> ||
30-
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
30+
| [100](https://leetcode-cn.com/problems/same-tree) | [相同的树](/solution/100-199/0100.same-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> ||
3131
| [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> |
Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,65 @@
1-
# [100. xxx](https://leetcode-cn.com/problems/recover-binary-search-tree)
1+
# [100. 相同的树](https://leetcode-cn.com/problems/same-tree/description/)
22

33
### 题目描述
44

5+
<p>给定两个二叉树,编写一个函数来检验它们是否相同。</p>
6+
7+
<p>如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。</p>
8+
9+
<p><strong>示例&nbsp;1:</strong></p>
10+
11+
<pre><strong>输入: </strong> 1 1
12+
/ \ / \
13+
2 3 2 3
14+
15+
[1,2,3], [1,2,3]
16+
17+
<strong>输出:</strong> true</pre>
18+
19+
<p><strong>示例 2:</strong></p>
20+
21+
<pre><strong>输入: </strong> 1 1
22+
/ \
23+
2 2
24+
25+
[1,2], [1,null,2]
26+
27+
<strong>输出:</strong> false
28+
</pre>
29+
30+
<p><strong>示例&nbsp;3:</strong></p>
31+
32+
<pre><strong>输入:</strong> 1 1
33+
/ \ / \
34+
2 1 1 2
35+
36+
[1,2,1], [1,1,2]
37+
38+
<strong>输出:</strong> false
39+
</pre>
540

641
### 解题思路
742

43+
1. 递归判断
844

945
### 具体解法
1046

11-
<!-- tabs:start -->
12-
1347
#### **Golang**
1448
```go
15-
49+
type TreeNode struct {
50+
Val int
51+
Left *TreeNode
52+
Right *TreeNode
53+
}
54+
func isSameTree(p *TreeNode, q *TreeNode) bool {
55+
if p == nil && q == nil {
56+
return true
57+
}
58+
if p == nil || q == nil {
59+
return false
60+
}
61+
return p.Val == q.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
62+
}
1663
```
1764

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=100 lang=golang
5+
*
6+
* [100] 相同的树
7+
*/
8+
// @lc code=start
9+
/**
10+
* Definition for a binary tree node.
11+
* type TreeNode struct {
12+
* Val int
13+
* Left *TreeNode
14+
* Right *TreeNode
15+
* }
16+
*/
17+
func isSameTree(p *TreeNode, q *TreeNode) bool {
18+
if p == nil && q == nil {
19+
return true
20+
}
21+
if p == nil || q == nil {
22+
return false
23+
}
24+
return p.Val == q.Val && isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
25+
}
26+
27+
// @lc code=end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type TreeNode struct {
8+
Val int
9+
Left *TreeNode
10+
Right *TreeNode
11+
}
12+
13+
func TestIsSameTree(t *testing.T) {
14+
var ret bool
15+
16+
p := &TreeNode{
17+
Val: 1,
18+
Left: &TreeNode{
19+
Val: 2,
20+
Left: nil,
21+
Right: nil,
22+
},
23+
Right: &TreeNode{
24+
Val: 2,
25+
Left: nil,
26+
Right: nil,
27+
},
28+
}
29+
30+
q := &TreeNode{
31+
Val: 1,
32+
Left: &TreeNode{
33+
Val: 2,
34+
Left: nil,
35+
Right: nil,
36+
},
37+
Right: &TreeNode{
38+
Val: 2,
39+
Left: nil,
40+
Right: nil,
41+
},
42+
}
43+
ret = true
44+
if ret != isSameTree(p, q) {
45+
t.Fatalf("case fails %v\n", ret)
46+
}
47+
48+
p1 := &TreeNode{
49+
Val: 1,
50+
Left: nil,
51+
Right: &TreeNode{
52+
Val: 2,
53+
Left: nil,
54+
Right: nil,
55+
},
56+
}
57+
58+
q1 := &TreeNode{
59+
Val: 1,
60+
Left: &TreeNode{
61+
Val: 2,
62+
Left: nil,
63+
Right: nil,
64+
},
65+
Right: &TreeNode{
66+
Val: 2,
67+
Left: nil,
68+
Right: nil,
69+
},
70+
}
71+
ret = false
72+
if ret != isSameTree(p1, q1) {
73+
t.Fatalf("case fails %v\n", ret)
74+
}
75+
76+
p2 := &TreeNode{}
77+
q2 := &TreeNode{}
78+
ret = true
79+
if ret != isSameTree(p2, q2) {
80+
t.Fatalf("case fails %v\n", ret)
81+
}
82+
}

‎solution/100-199/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
- 📚&nbsp;&nbsp;题目列表
88
- [100 - 199题](index-tags.md)
9-
- [100. 相同的树](solution/100-199/0100.same-tree/)
9+
- [100. 相同的树](solution/100-199/0100.same-tree/)
1010
- [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/)

0 commit comments

Comments
(0)

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