-
Notifications
You must be signed in to change notification settings - Fork 2
/
572. Subtree of Another Tree.py
111 lines (97 loc) · 3.25 KB
/
572. Subtree of Another Tree.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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.
# Example 1:
# Given tree s:
# 3
# / \
# 4 5
# / \
# 1 2
# Given tree t:
# 4
# / \
# 1 2
# Return true, because t has the same structure and node values with a subtree of s.
# Example 2:
# Given tree s:
# 3
# / \
# 4 5
# / \
# 1 2
# /
# 0
# Given tree t:
# 4
# / \
# 1 2
# Hints:
# Which approach is better here- recursive or iterative?
# If recursive approach is better, can you write recursive function with its parameters?
# Two trees s and t are said to be identical if their root values are same
# and their left and right subtrees are identical. Can you write this in form of recursive formulae?
# Recursive formulae can be:
# isIdentical(s,t)= s.val==t.val AND isIdentical(s.left,t.left) AND isIdentical(s.right,t.right)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
# M1. 递归先序遍历
# print(self.preorder(s),len(self.preorder(s)))
# print(self.preorder(t),len(self.preorder(t)))
return self.preorder(t) in self.preorder(s)
def preorder(self, root):
if not root:
return "*"
else:
# “ ” for case like [12] and [2]
# You can also note that we've added a '#' before every considering every value.
# If this isn't done, the trees of the form s:[23, 4, 5] and t:[3, 4, 5] will also give a true result
# since the preorder string of the t("23 4 lnull rull 5 lnull rnull") will be a substring of the preorder string of s("3 4 lnull rull 5 lnull rnull").
# Adding a '#' before the node's value solves this problem.
return " " + str(root.val) + self.preorder(root.left) + self.preorder(root.right)
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
# M2. 模拟比较
# for case middle
if not s or not t:
return False
isIdentical = False
if s.val == t.val:
isIdentical = self.tree_comparor(s,t)
if isIdentical:
return True
else:
return self.isSubtree(s.left,t) or self.isSubtree(s.right,t)
def tree_comparor(self, t1, t2):
if t1 is None or t2 is None:
if t1 is None and t2 is None:
return True
else:
return False
if t1.val != t2.val:
return False
else:
return self.tree_comparor(t1.left,t2.left) and self.tree_comparor(t1.right,t2.right)