-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeLevelOrderTraversal.py
More file actions
359 lines (274 loc) Β· 8.16 KB
/
binaryTreeLevelOrderTraversal.py
File metadata and controls
359 lines (274 loc) Β· 8.16 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#neetcode 29
class Solution:
def levelOrder(self, root):
# creating a list to store the result
result = []
# creating a queue for bfs
q = collections.deque()
q.append(root)
while q:
lenQ = len(q)
level = []
for i in range(lenQ):
# we pop the queue from the left side
node = q.popleft()
# if node is not empty
if node:
level.append(node.val)
q.append(node.left)
q.append(node.right)
if level:
result.append(level)
return result
# I'm about to **teach this like you're five**.
# And we're going **deep**β¦ like *NASA-mission-to-Mars deep*.
# Let's crack this code open, **line by line**, **visually**, and **in plain human language**, no geek talk!
# ---
# ## π― What's the code about?
# It's doing a **Level Order Traversal** of a **Binary Tree**.
# Imagine you're in a building and you're **visiting floors**, **one floor at a time**, **left to right**, **top to bottom**.
# That's level order traversal.
# > TL;DR:
# You're going **floor by floor**, **row by row**.
# ---
# ## π’ Visualization Example of a Tree (Think of it as a building)
# ```
# 1 <- Floor 1 (Level 0)
# / \
# 2 3 <- Floor 2 (Level 1)
# / \ \
# 4 5 6 <- Floor 3 (Level 2)
# ```
# If you **traverse floor by floor**, it looks like:
# ```
# [ [1], [2, 3], [4, 5, 6] ]
# ```
# ---
# ## π§ Now let's BREAK DOWN the CODE LINE BY LINE
# And **visualize** where your brain should be at every step.
# ---
# ---
# ## π Code - The Top Part
# ```python
# class Solution:
# ```
# π§ Think of this as **creating a box** that will hold our solution.
# It's like **naming a recipe**.
# ---
# ```python
# def levelOrder(self, root: TreeNode) -> List[List[int]]:
# ```
# ### What does this do?
# π£οΈ _"Hey! I'm making a **function** called `levelOrder` that will give me **levels** in a tree, one by one."_
# You give me a **tree** (`root`), and I give you **lists of lists** of numbers, each representing one **floor** in the tree.
# ---
# ```python
# res = []
# ```
# - `res` is where we'll **store** the answer.
# - Picture `res` like your **notebook** where you're writing down **who lives on each floor** of the building.
# ---
# ---
# ## πͺ£ Make a Queue!
# ```python
# q = collections.deque()
# ```
# - This is a **queue**, like people **lining up** at a counter.
# - **First in, first out (FIFO)**.
# Think of **waiting in line at KFC**, whoever comes first gets served first.
# ---
# ```python
# q.append(root)
# ```
# - We **put the root** (the big boss node at the top of the tree) **into the line**.
# - π§ Imagine you **open the building door**, and the **first person** you meet is **Node 1 (root)**.
# ---
# ---
# ## π Walk the Building, Floor by Floor!
# ```python
# while q:
# ```
# π£οΈ _"As long as there are people in the queue (someone to visit), keep going!"_
# At this point, we are **standing in front of the building**
# - We'll **visit each floor**,
# - Check who's there
# - Then **add their children** (if any) to the **queue** for the **next visit**.
# ---
# ---
# ## π’ Get the number of people on this floor
# ```python
# qLen = len(q)
# ```
# - This tells us **how many people** are currently in line.
# - **These are the people on this floor!**
# π§ Example:
# - You come to Floor 1
# - You see **only 1 person in line**, that's Node `1`.
# `qLen = 1`
# ---
# ---
# ## ποΈ Start a list for this level!
# ```python
# level = []
# ```
# - This list will hold **all the people you meet** on **this floor**.
# - Picture it like writing **everyone's name** on this floor in your **notebook page**.
# ---
# ---
# ## πΆββοΈ Visit Everyone on This Level
# ```python
# for i in range(qLen):
# ```
# - You're saying **"For everyone on this floor, do this..."**
# - We loop through **everyone on this level**
# - Example:
# `qLen = 2` β you loop **two times** for people on this floor.
# ---
# ---
# ## π Meet the next person in line
# ```python
# node = q.popleft()
# ```
# - You **take the first person out** of the queue.
# - FIFO: **First person who entered, gets served first**.
# Example:
# - Queue was `[1]`
# - `node = 1`
# Now `q` is empty until we **add children** later.
# ---
# ---
# ## π¨οΈ If there's actually someone there
# ```python
# if node:
# ```
# - Double-check you actually got a **real node** and not a **ghost (None)**.
# ---
# ---
# ## βοΈ Write down their name!
# ```python
# level.append(node.val)
# ```
# - You **write the node's value** down on your **list for this level**.
# Visualization:
# - You met `node = 1`
# - You write down `1` in the level list.
# `level = [1]`
# ---
# ---
# ## πΆ Add their kids to the next floor queue!
# ```python
# q.append(node.left)
# q.append(node.right)
# ```
# - You check if this person has **kids (left and right)**
# - If they do, you **add them to the queue**
# - You'll **visit them** on the **next loop**, i.e., next floor.
# Example:
# - Node `1` has kids: `2` and `3`
# - `q` becomes `[2, 3]`
# π§ **Next time** we go back to `while q`, we're meeting `2` and `3`.
# ---
# ---
# ## ποΈ After visiting everyone on the floor, save the level!
# ```python
# if level:
# res.append(level)
# ```
# - After you finish **everyone on this floor**,
# - You **add the list** to your **notebook (`res`)**
# Example:
# - `level = [1]`
# - Now `res = [[1]]`
# ---
# ---
# ## π Done! Return the notebook
# ```python
# return res
# ```
# - Return your **list of lists**
# - This shows **who's on each floor**, in order.
# ---
# ---
# ---
# ## π’ Real-Life Visualization Example
# ### Given this tree:
# ```
# 1
# / \
# 2 3
# / \ \
# 4 5 6
# ```
# - Start at root `1`
# - Queue = `[1]`
# ---
# ### First while loop:
# - qLen = 1
# - level = []
# - Visit `1`
# - Add `1` to level β `[1]`
# - Add `2` and `3` to queue
# - Queue = `[2, 3]`
# - Add level `[1]` to res
# - res = `[[1]]`
# ---
# ### Second while loop:
# - qLen = 2
# - level = []
# - Visit `2`
# - Add `2` to level β `[2]`
# - Add `4` and `5` to queue
# - Queue = `[3, 4, 5]`
# - Visit `3`
# - Add `3` to level β `[2, 3]`
# - Add `6` to queue
# - Queue = `[4, 5, 6]`
# - Add level `[2, 3]` to res
# - res = `[[1], [2, 3]]`
# ---
# ### Third while loop:
# - qLen = 3
# - level = []
# - Visit `4`
# - Add `4` to level β `[4]`
# - No kids β don't add anything
# - Visit `5`
# - Add `5` to level β `[4, 5]`
# - No kids
# - Visit `6`
# - Add `6` to level β `[4, 5, 6]`
# - No kids
# - Queue is empty
# - Add level `[4, 5, 6]` to res
# - res = `[[1], [2, 3], [4, 5, 6]]`
# ---
# ---
# ---
# ## β
Full Code Again for Context
# ```python
# class Solution:
# def levelOrder(self, root: TreeNode) -> List[List[int]]:
# res = [] # Notebook to store levels
# q = collections.deque() # Our line (queue)
# q.append(root) # Start with the root
# while q: # While there are people in line
# qLen = len(q) # How many on this floor?
# level = [] # New page in notebook
# for i in range(qLen): # Visit everyone on this floor
# node = q.popleft() # Meet the next person
# if node: # If they exist
# level.append(node.val) # Write their name
# q.append(node.left) # Add their left child
# q.append(node.right) # Add their right child
# if level: # Finished this floor
# res.append(level) # Save it to notebook
# return res # Give me the notebook!
# ```
# ---
# ---
# ## π§ Mental Model to Remember
# - **Queue** is like **people waiting in line**
# - Each **while loop** is a **floor visit**
# - `qLen` tells you how many **rooms** on the **current floor**
# - `level` is your **guest list** for this floor
# - `res` is your **full guest log**, floor by floor