-
Notifications
You must be signed in to change notification settings - Fork 44
/
binary-tree-right-side-view.py
267 lines (234 loc) · 6.96 KB
/
binary-tree-right-side-view.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
199. Binary Tree Right Side View
Medium
Given the root of 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 1:
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
Example 2:
Input: root = [1,null,3]
Output: [1,3]
Example 3:
Input: root = []
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
"""
# V0
# IDEA : DFS
class Solution(object):
def rightSideView(self, root):
def dfs(root, _layer):
if len(res) <= _layer:
res.append([])
if root:
res[_layer].append(root.val)
if not root:
return
if root.left:
dfs(root.left, _layer+1)
if root.right:
dfs(root.right, _layer+1)
# edge case
if not root:
return
res = [[]]
_layer = 0
dfs(root, _layer)
return [x[-1] for x in res]
# V0'
# IDEA : BFS
class Solution(object):
def rightSideView(self, root):
# edge case
if not root:
return []
res = [[]]
_layer = 0
q = [[root,_layer]]
while q:
for i in range(len(q)):
tmp, _layer = q.pop(0)
if len(res) <= _layer:
res.append([])
res[_layer].append(tmp.val)
if tmp.right:
q.append([tmp.right, _layer+1])
if tmp.left:
q.append([tmp.left, _layer+1])
ans = [i[0] for i in res]
return ans
# V0'
# IDEA : DFS
class Solution(object):
def rightSideView(self, root):
def dfs(root, layer):
if not root:
return
if len(res) <= layer+1:
#if len(res) == layer: # this works as well
res.append([])
res[layer].append(root.val)
if root.right:
dfs(root.right, layer+1)
if root.left:
dfs(root.left, layer+1)
if not root:
return []
res =[[]]
dfs(root, 0)
return [x[0] for x in res if len(x) > 0]
# V0''
# IDEA : BFS
class Solution(object):
def rightSideView(self, root):
if not root:
return []
q = []
layer=0
res = []
### NOTE : we need layer, so we design our `node` in queue as (root, layer)
q.append((root, layer))
while q:
for i in range(len(q)):
tmp = q.pop()
### NOTE : every time we pop root
# -> and do the root.left, root.right op below
root = tmp[0]
layer = tmp[1]
if len(res) == layer:
res.append([])
res[layer].append(root.val)
if root.right:
q.append((root.right, layer+1))
if root.left:
q.append((root.left, layer+1))
return [x[-1] for x in res]
# V0'''
# IDEA : DFS
class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = []
self.levelOrder(root, 0, res)
return [level[-1] for level in res]
def levelOrder(self, root, level, res):
if not root: return
if len(res) == level: res.append([])
res[level].append(root.val)
if root.left: self.levelOrder(root.left, level + 1, res)
if root.right: self.levelOrder(root.right, level + 1, res)
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79557632
# 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]
"""
res = []
self.levelOrder(root, 0, res)
return [level[-1] for level in res]
def levelOrder(self, root, level, res):
if not root: return
if len(res) == level: res.append([])
res[level].append(root.val)
if root.left: self.levelOrder(root.left, level + 1, res)
if root.right: self.levelOrder(root.right, level + 1, res)
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/79557632
# 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]
"""
res = []
if not root: return res
queue = collections.deque()
queue.append(root)
while queue:
res.append(queue[-1].val)
for i in range(len(queue)):
node = queue.popleft()
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return res
# V1'
# https://www.jiuzhang.com/solution/binary-tree-right-side-view/#tag-highlight-lang-python
class Solution:
"""
@param root: the root of the given tree
@return: the values of the nodes you can see ordered from top to bottom
"""
def rightSideView(self, root):
# write your code here
def collect(node, depth):
if node:
if depth == len(view):
view.append(node.val)
collect(node.right, depth + 1)
collect(node.left, depth + 1)
view = []
collect(root, 0)
return view
# V2
# Time: O(n)
# Space: O(h)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# @param root, a tree node
# @return a list of integers
def rightSideView(self, root):
result = []
self.rightSideViewDFS(root, 1, result)
return result
def rightSideViewDFS(self, node, depth, result):
if not node:
return
if depth > len(result):
result.append(node.val)
self.rightSideViewDFS(node.right, depth+1, result)
self.rightSideViewDFS(node.left, depth+1, result)
# BFS solution
# Time: O(n)
# Space: O(n)
class Solution2(object):
# @param root, a tree node
# @return a list of integers
def rightSideView(self, root):
if root is None:
return []
result, current = [], [root]
while current:
next_level = []
for node in current:
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
result.append(node.val)
current = next_level
return result