Skip to content

Commit d7be863

Browse files
committed
添加 problem 572
1 parent c8a5f73 commit d7be863

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
func isSubtree(s *TreeNode, t *TreeNode) bool {
12+
if isSameTree(s, t) {
13+
return true
14+
}
15+
if s == nil {
16+
return false
17+
}
18+
if isSubtree(s.Left, t) || isSubtree(s.Right, t) {
19+
return true
20+
}
21+
return false
22+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question572 struct {
9+
para572
10+
ans572
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para572 struct {
16+
s []int
17+
t []int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans572 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem572(t *testing.T) {
27+
28+
qs := []question572{
29+
30+
question572{
31+
para572{[]int{}, []int{}},
32+
ans572{false},
33+
},
34+
35+
question572{
36+
para572{[]int{3, 4, 5, 1, 2}, []int{4, 1, 2}},
37+
ans572{true},
38+
},
39+
40+
question572{
41+
para572{[]int{1, 1}, []int{1}},
42+
ans572{true},
43+
},
44+
45+
question572{
46+
para572{[]int{1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, 2}, []int{1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, NULL, 1, 2}},
47+
ans572{true},
48+
},
49+
}
50+
51+
fmt.Printf("------------------------Leetcode Problem 572------------------------\n")
52+
53+
for _, q := range qs {
54+
_, p := q.ans572, q.para572
55+
fmt.Printf("【input】:%v ", p)
56+
roots := Ints2TreeNode(p.s)
57+
roott := Ints2TreeNode(p.t)
58+
fmt.Printf("【output】:%v \n", isSubtree(roots, roott))
59+
}
60+
fmt.Printf("\n\n\n")
61+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [572. Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)
2+
3+
4+
## 题目:
5+
6+
Given two non-empty binary trees **s** and **t**, check whether tree **t** has exactly the same structure and node values with a subtree of **s**. A subtree of **s** is a tree consists of a node in **s** and all of this node's descendants. The tree **s** could also be considered as a subtree of itself.
7+
8+
**Example 1:** Given tree s:
9+
10+
3
11+
/ \
12+
4 5
13+
/ \
14+
1 2
15+
16+
Given tree t:
17+
18+
4
19+
/ \
20+
1 2
21+
22+
Return **true**, because t has the same structure and node values with a subtree of s.
23+
24+
**Example 2:**Given tree s:
25+
26+
3
27+
/ \
28+
4 5
29+
/ \
30+
1 2
31+
/
32+
0
33+
34+
Given tree t:
35+
36+
4
37+
/ \
38+
1 2
39+
40+
Return **false**.
41+
42+
43+
## 题目大意
44+
45+
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
46+
47+
48+
## 解题思路
49+
50+
51+
- 给出 2 棵树 `s``t`,要求判断 `t` 是否是 `s` 的子树🌲。
52+
- 这一题比较简单,针对 3 种情况依次递归判断,第一种情况 `s``t` 是完全一样的两棵树,第二种情况 `t``s` 左子树中的子树,第三种情况 `t``s` 右子树中的子树。第一种情况判断两棵数是否完全一致是第 100 题的原题。
53+

0 commit comments

Comments
 (0)