-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path199.BinaryTreeRightSideView.py
48 lines (44 loc) · 1.42 KB
/
199.BinaryTreeRightSideView.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
"""
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.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
"""
#Difficulty: Medium
#211 / 211 test cases passed.
#Runtime: 44 ms
#Memory Usage: 14 MB
#Runtime: 44 ms, faster than 28.97% of Python3 online submissions for Binary Tree Right Side View.
#Memory Usage: 14 MB, less than 9.61% of Python3 online submissions for Binary Tree Right Side View.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return root
queue = [root]
right_side_view = []
while queue:
length = len(queue)
level = []
while length:
node = queue.pop(0)
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
length -= 1
right_side_view.append(level[-1])
return right_side_view