-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0102-binary-tree-level-order-traversal.py
More file actions
64 lines (51 loc) · 1.57 KB
/
0102-binary-tree-level-order-traversal.py
File metadata and controls
64 lines (51 loc) · 1.57 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
64
# time complexity: O(n)
# space complexity: O(n)
from collections import deque
from typing import List, Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
result: List[List[int]] = []
def bfs(node: Optional[TreeNode], level: int):
if node is None:
return
if len(result) == level:
result.append([])
result[level].append(node.val)
if node.left:
bfs(node.left, level + 1)
if node.right:
bfs(node.right, level + 1)
bfs(root, 0)
return result
# time complexity: O(n)
# space complexity: O(n)
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
levels = []
if not root:
return levels
level = 0
queue = deque([root])
while queue:
levels.append([])
levelLen = len(queue)
for _ in range(levelLen):
node = queue.popleft()
levels[level].append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
level += 1
return levels
root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20)
root.right.left = TreeNode(15)
root.right.right = TreeNode(7)
print(Solution().levelOrder(root))