-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy path572_Subtree_of_Another_Tree.py
39 lines (34 loc) · 1.24 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
# 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):
# https://leetcode.com/problems/subtree-of-another-tree/solution/
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
s_res = self.preorder(s, True)
t_res = self.preorder(t, True)
return t_res in s_res
def preorder(self, root, isLeft):
if root is None:
if isLeft:
return "lnull"
else:
return "rnull"
return "#" + str(root.val) + " " + self.preorder(root.left, True) + " " + self.preorder(root.right, False)
# def isSubtree(self, s, t):
# return self.traverse(s, t)
# def equals(self, x, y):
# if x is None and y is None:
# return True
# if x is None or y is None:
# return False
# return x.val == y.val and self.equals(x.left, y.left) and self.equals(x.right, y.right)
# def traverse(self, s, t):
# return s is not None and (self.equals(s, t) or self.traverse(s.left, t) or self.traverse(s.right, t))