-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102_binaryTreeLevelOrderTraversal.py
52 lines (39 loc) · 1.54 KB
/
102_binaryTreeLevelOrderTraversal.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
40
41
42
43
44
45
46
47
48
49
50
51
52
# 102. Binary Tree Level Order Traversal
# 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
# INPUT: [3,9,20,null,null,15,7]
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
# BFS GIVING [[3],[9,20],[15,7]] AS OUTPUT
# DEQUE STORING NODES AND LEVEL NUMBER.
# RESULT STORING VALUES. UPDATES ACCORDING TO THE LEVEL NUMBER.
if not root: return []
result, q = [], deque([(root, 1)]) # result = values, deque = nodes, level no.
while q:
curr, j = q.popleft()
if curr.left: q.append((curr.left, j + 1))
if curr.right: q.append((curr.right, j + 1))
if len(result) < j: result.append([curr.val])
else: result[-1].append(curr.val)
return result
# # NORMAL BFS GIVING SINGLE LIST OF VALUES AS OUTPUT
# # I.E., BFS GIVING [3,9,20,15,7]
# nodeStack = []
# l = []
# if(root==None):
# return None
# else:
# nodeStack.append(root)
# while(nodeStack):
# node = nodeStack.pop(0)
# l.append(node.val)
# if(node.left!=None):
# nodeStack.append(node.left)
# if(node.right!=None):
# nodeStack.append(node.right)
# print(l)
# return l