-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path26_SubstructureInTree.py
66 lines (49 loc) · 1.37 KB
/
26_SubstructureInTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# -*- coding: utf-8 -*-
# @File : 26_SubstructureInTree.py
# @Date : 2020-11-01
# @Author : tc
"""
剑指 Offer 26. 树的子结构
输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
B是A的子结构, 即 A中有出现和B相同的结构和节点值。
例如:
给定的树 A:
3
/ \
4 5
/ \
1 2
给定的树 B:
4
/
1
返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。
示例 1:
输入:A = [1,2,3], B = [3,1]
输出:false
示例 2:
输入:A = [3,4,5,1,2], B = [4,1]
输出:true
限制:
0 <= 节点个数 <= 10000
先序遍历 + 包含判断
参考:https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/solution/mian-shi-ti-26-shu-de-zi-jie-gou-xian-xu-bian-li-p/
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSubStructure(self, A: TreeNode, B: TreeNode) -> bool:
if not A or not B:
return False
return self.dfs(A, B) or self.isSubStructure(A.left, B) or self.isSubStructure(A.right, B)
def dfs(self, A, B):
if not B:
return True
if not A:
return False
return A.val == B.val and self.dfs(A.left, B.left) and self.dfs(A.right, B.right)
if __name__ == '__main__':
pass