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 8fd4c47

Browse files
Merge pull request doocs#234 from ElectricBubble/master
Add Solution.go and README.md for 0100.Same Tree
2 parents 6d74f4f + 1dc338e commit 8fd4c47

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

‎solution/0100.Same Tree/README.md‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## 相同的树
2+
### 题目描述
3+
4+
给定两个二叉树,编写一个函数来检验它们是否相同。
5+
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
6+
7+
示例 1:
8+
```
9+
输入: 1 1
10+
/ \ / \
11+
2 3 2 3
12+
13+
[1,2,3], [1,2,3]
14+
15+
输出: true
16+
```
17+
18+
19+
示例 2:
20+
```
21+
输入: 1 1
22+
/ \
23+
2 2
24+
25+
[1,2], [1,null,2]
26+
27+
输出: false
28+
```
29+
30+
31+
示例 3:
32+
```
33+
输入: 1 1
34+
/ \ / \
35+
2 1 1 2
36+
37+
[1,2,1], [1,1,2]
38+
39+
输出: false
40+
```

‎solution/0100.Same Tree/Solution.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func isSameTree(p *TreeNode, q *TreeNode) bool {
2+
if p == nil && q == nil {
3+
return true
4+
}
5+
if p == nil || q == nil {
6+
return false
7+
}
8+
if p.Val != q.Val {
9+
return false
10+
}
11+
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
12+
}

0 commit comments

Comments
(0)

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