-
Notifications
You must be signed in to change notification settings - Fork 0
/
107.py
35 lines (35 loc) · 846 Bytes
/
107.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
# This problem was asked by Microsoft.
# Print the nodes in a binary tree level-wise. For example, the following should
# print 7, 5, 1, 4, 3, 1
# 7
# / \
# 5 1
# / \ \
# 4 3 1
####
class Node:
def __init__(self, val = None, left = None, right = None):
self.left = left
self.right = right
self.val = val
n1 = Node(val = 4)
n2 = Node(val = 3)
n3 = Node(val = 1)
n4 = Node(left = n1, right = n2, val = 5)
n5 = Node(right = n3, val = 1)
s = Node(left = n4, right = n5, val = 7)
####
# Simple BFS returns the nodes in level order.
def levelwise(root):
q = [root]
cur = None
while q:
cur = q.pop(0)
yield cur
if cur.left:
q.append(cur.left)
if cur.right:
q.append(cur.right)
####
sol = [x.val for x in levelwise(s)]
print(sol)