-
Notifications
You must be signed in to change notification settings - Fork 0
/
872-easy.py
73 lines (63 loc) · 2.05 KB
/
872-easy.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
'''
请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。
【解题】70%,90%
【卡点】
1. 以为不能用树的遍历。
2. 先序,后续,中序遍历应该都可以。
3. 非递归遍历还有有些难度。用两个if ,一个是左孩子入栈,一个是出栈。
'''
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def leafSimilar(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
# 【递归】 取出叶子节点,并且转换成字符串的数组。下面好用join方法。
# res1 = self.dfs(root1)
# res2 = self.dfs(root2)
#【非递归】自己构建堆栈
res1 = self.depthFirst(root1)
res2 = self.depthFirst(root2)
print(res1)
print(res2)
#这里直接比较转成的字符串是否相等
return "-".join(res1) == "-".join(res2)
'''
递归遍历
'''
def dfs(self, root):
res = []
if root.left is not None:
res = res + self.dfs(root.left)
if root.right is not None:
res = res + self.dfs(root.right)
if root.right is None and root.left is None:
res.append(str(root.val))
return res
'''
非递归
'''
# 深度优先遍历
def depthFirst(self,root):
ls = []
res = []
while len(ls)>0 or root is not None:
if root is not None:
# print("l:",root.val)
ls.append(root)
root = root.left
elif len(ls)>0:
tmp = ls.pop()
# print(ls)
if tmp.right is None and tmp.left is None:
res.append(str(tmp.val))
if tmp.right is not None:
root=tmp.right
return res