-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode 102 - level order traversal of tree.py
39 lines (31 loc) · 1.24 KB
/
leetcode 102 - level order traversal of tree.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
# binary tree level order traversal | leetcode 102 | https://leetcode.com/problems/binary-tree-level-order-traversal/
# order: from left to right, level by level
# method: breadth first search
#Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
# l to r, level by level
def levelOrder(self, root):
res = []
tempQ = []
# queue to track visits
tempQ.append(root)
LtempQ = len(tempQ)
# keep iterating till:
# the track queue is empty
while LtempQ is not 0:
LtempQ = len(tempQ)
level = []
for i in range(LtempQ):
node = tempQ.pop(0) # pop this node from queue (visited)
if node is not None:
level.append(node.val) # add this node to the level
tempQ.append(node.left) # add left child to queue (to visit)
tempQ.append(node.right) # add right child to queue (to visit)
if len(level) is not 0:
res.append(level)
return res