Skip to content

Commit e9ac653

Browse files
committed
Update zigzagtraversal comments
1 parent db9210b commit e9ac653

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

Python/Binary_Tree_Zig_Zag_Traversal.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44
#Difficulty: Medium
55

66
def zigzagLevelOrder(root):
7-
"""
8-
:type root: TreeNode
9-
:rtype: List[List[int]]
10-
"""
7+
#If the root is null, return an empty list
118
if not root: return []
9+
#Create variables r (result), q (queue), and c (current queue)
1210
r, q, c = [[]], [root], []
11+
#Keep track of if we're going left or right in our zigzag traversal
1312
zig = False
13+
#As long as the queue is not empty
1414
while q:
15+
#For each item in the queue
1516
for i in q:
17+
#Append item to the end of the result list
1618
r[-1] += [i.val]
19+
#If left and right children exist, add them to current queue in order of left to right
1720
if i.left: c += [i.left]
1821
if i.right: c += [i.right]
22+
#Reset queue to current queue and current queue to an empty list
1923
q, c = c, []
24+
#If we're going right to left, reverse the last item in our results list
2025
if zig:
2126
r[-1] = r[-1][::-1]
27+
#Negate out boolen so we go the opposite direction next time
2228
zig = not zig
29+
#If our queue still is non null, add an empty list to our result because we sill have entries to fill
2330
if q: r += [[]]
2431
return r

0 commit comments

Comments
 (0)