-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel-order-alternating.py
More file actions
31 lines (30 loc) · 987 Bytes
/
level-order-alternating.py
File metadata and controls
31 lines (30 loc) · 987 Bytes
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
# https://binarysearch.com/problems/Level-Order-Alternating
# class Tree:
# def __init__(self, val, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def solve(self, root):
curr_level = [root]
left_to_right = True
result = []
while True:
if left_to_right:
for node in curr_level:
result.append(node.val)
else:
for node in reversed(curr_level):
result.append(node.val)
next_level = []
for node in curr_level:
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
if len(next_level) > 0:
curr_level = next_level
left_to_right = not left_to_right
else:
break
return result