-
Notifications
You must be signed in to change notification settings - Fork 0
/
102. Binary Tree Level Order Traversal.py
47 lines (43 loc) · 1.18 KB
/
102. Binary Tree Level Order Traversal.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
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution(object):
def levelOrder(self, root):
"""
:type root: Optional[TreeNode]
:rtype: List[List[int]]
"""
queue = []
queue2 = []
output = []
if not root:
return []
queue.append(root)
output.append([root.val])
tmp = []
while queue:
current = queue.pop(0)
if current.left:
tmp.append(current.left.val)
queue2.append(current.left)
if current.right:
tmp.append(current.right.val)
queue2.append(current.right)
if not queue:
if tmp:
output.append(tmp)
tmp = []
queue = queue2
queue2 = []
return output
n15 = TreeNode(15)
n7 = TreeNode(7)
n20 = TreeNode(20, n15, n7)
n9 = TreeNode(9)
n3 = TreeNode(3, n9, n20)
s = Solution()
res = s.levelOrder(n3)
print(res)