-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path199.py
More file actions
44 lines (40 loc) · 1.14 KB
/
199.py
File metadata and controls
44 lines (40 loc) · 1.14 KB
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
class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ans = []
if not root:
return []
self.post(root, 1, ans)
return ans
def post(self, node, depth, ans):
if node is None:
return
if len(ans) < depth:
ans.append(node.val)
if node.right:
self.post(node.right, depth+1, ans)
if node.left:
self.post(node.left, depth+1, ans)
def rightSideView(self, root):
"""
这种解法符合我的思想,当时我不知道如何实现。
看了别人代码和知道了deque之后,会的
"""
if root is None:
return []
from collections import deque
de = deque()
de.append(root)
ans = []
while de:
ans.append(de[-1].val)
for i in range(len(de)):
p = de.popleft()
if p.left:
de.append(p.left)
if p.right:
de.append(p.right)
return ans