-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0199_binary_tree_right_side_view.py
More file actions
122 lines (106 loc) · 3.19 KB
/
0199_binary_tree_right_side_view.py
File metadata and controls
122 lines (106 loc) · 3.19 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
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
#------------------------------------------------------------------------------
# Question: 0199_binary_tree_right_side_view.py
#------------------------------------------------------------------------------
# tags:
'''
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 <---
'''
#------------------------------------------------------------------------------
# Solutions
#------------------------------------------------------------------------------
from typing import *
class Solution:
'''
1
/ \
2 3
\
4
/ \
6 5
ans = 1 3 4 5
r d
node = (2,1)
cur_d = 2
result = [1, 3, 4]
proccess q:
get from q (node, depth)
if the depth of node is equal to current depth in question, append node to result
, then update current depth += 1
add right child
add left child
'''
def rightSideView(self, root: TreeNode) -> List[int]:
if root is None:
return []
q = [(root, 0)]
cur_d = 0
result = []
while q:
q_size = len(q)
for _ in range(q_size):
node, depth = q.pop(0)
if node:
if depth == cur_d:
result.append(node.val)
cur_d += 1
q.append((node.left, depth + 1))
q.append((node.right, depth + 1))
return result
def rightSideView(self, root: TreeNode) -> List[int]:
if root is None:
return []
q = [(root, 0)]
result = []
while q:
q_size = len(q)
for i in range(q_size):
node, depth = q.pop(0)
if i == 0:
result.append(node.val)
if node.left:
q.append((node.left, depth + 1))
if node.right:
q.append((node.right, depth + 1))
return result
# 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 root is None:
return []
q = [(root, 0)]
result = []
while q:
q_size = len(q)
for i in range(q_size):
node, level = q.pop(0)
if i == q_size-1:
result.append(node.val)
if node.left:
q.append((node.left, level+1))
if node.right:
q.append((node.right, level+1))
return result
#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------
import unittest
class TestSolution(unittest.TestCase):
def test_simple(self):
s = Solution()
self.assertEqual(True, True)
unittest.main(verbosity=2)