-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversal.py
More file actions
63 lines (52 loc) · 2 KB
/
Copy pathBinaryTreeLevelOrderTraversal.py
File metadata and controls
63 lines (52 loc) · 2 KB
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
53
54
55
56
57
58
59
60
61
62
63
# 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
from collections import deque
class Solution:
# def levelOrder(self, root):
# ans, level = [], [root]
# while root and level:
# ans.append([node.val for node in level])
# LRpair = [(node.left, node.right) for node in level]
# level = [leaf for LR in LRpair for leaf in LR if leaf]
# return ans
# def levelOrder(self, root):
# if not root: return []
# queue, res = deque([root]), []
# while queue:
# cur_level, size = [], len(queue)
# for i in range(size):
# node = queue.popleft()
# if node.left:
# queue.append(node.left)
# if node.right:
# queue.append(node.right)
# cur_level.append(node.val)
# res.append(cur_level)
# return res
def levelOrder(self, root: TreeNode) -> List[List[int]]:
# when you have root, print its value
# and then put its left and right nodes into the queue
# notice python handles linked list in a wierd way, so
# you must use the length to limit the copying.
if not root:
return []
q, nq, length = [], [root], 0
result, cur_result = [], []
while nq:
q = nq
nq, cur_result = [], []
length = len(q)
for i in range(length):
cur_node = q.pop(0)
if cur_node:
if cur_node.left:
nq.append(cur_node.left)
if cur_node.right:
nq.append(cur_node.right)
cur_result += [cur_node.val]
result += [cur_result]
return result