-
Notifications
You must be signed in to change notification settings - Fork 43
/
leaf-similar-trees.py
134 lines (124 loc) · 3.42 KB
/
leaf-similar-trees.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/81748617
# Inorder
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
leaves1 = []
leaves2 = []
self.inOrder(root1, leaves1)
self.inOrder(root2, leaves2)
return leaves1 == leaves2
def inOrder(self, root, leaves):
if not root:
return
self.inOrder(root.left, leaves)
if not root.left and not root.right:
leaves.append(root.val)
self.inOrder(root.right, leaves)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/81748617
# pastOrder
class Solution:
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
return self.getLeafs(root1) == self.getLeafs(root2)
def getLeafs(self, root):
res = []
if not root:
return res
if not root.left and not root.right:
return [root.val]
res.extend(self.getLeafs(root.left))
res.extend(self.getLeafs(root.right))
return res
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/81748617
# postOrder
class Solution:
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
return self.postOrder(root1) == self.postOrder(root2)
def postOrder(self, root):
stack = []
stack.append(root)
res = []
while stack:
node = stack.pop()
if not node: continue
stack.append(node.left)
stack.append(node.right)
if not node.left and not node.right:
res.append(node.val)
return res
# V1'''
# https://blog.csdn.net/fuxuemingzhu/article/details/81748617
# looping
class Solution:
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
return self.preOrder(root1) == self.preOrder(root2)
def preOrder(self, root):
stack = []
stack.append(root)
res = []
while stack:
node = stack.pop()
if not node: continue
if not node.left and not node.right:
res.append(node.val)
stack.append(node.left)
stack.append(node.right)
return res
# V2
# Time: O(n)
# Space: O(h)
import itertools
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# V2'
class Solution(object):
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
def dfs(node):
if not node:
return
if not node.left and not node.right:
yield node.val
for i in dfs(node.left):
yield i
for i in dfs(node.right):
yield i
return all(a == b for a, b in
itertools.izip_longest(dfs(root1), dfs(root2)))