-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path199_BinaryTreeRightSideView.py
More file actions
66 lines (52 loc) · 1.53 KB
/
Copy path199_BinaryTreeRightSideView.py
File metadata and controls
66 lines (52 loc) · 1.53 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# coding: utf8
"""
题目链接: https://leetcode.com/problems/binary-tree-right-side-view/description.
题目描述:
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see
ordered from top to bottom.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
"""
# 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 rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
ans = []
self.bfs(root, ans)
return ans
def bfs(self, root, ans):
queue = []
front = 0
rear = 1
queue.append(root)
while front < rear:
last = rear
# 加入每层的最右节点
ans.append(queue[last - 1].val)
while front < last:
root = queue[front]
front += 1
if root.left:
queue.append(root.left)
rear += 1
if root.right:
queue.append(root.right)
rear += 1