-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path314_Binary_Tree_Vertical_Order_Traversal.py
More file actions
60 lines (47 loc) · 1.94 KB
/
314_Binary_Tree_Vertical_Order_Traversal.py
File metadata and controls
60 lines (47 loc) · 1.94 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
# 2 Possible Solutions
# 1. BFS with Sort
# 2. BFS with no sort.
# 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:
# Time: O(NlogN), Space: O(N)
def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return none
# Dictionary used to keep track of column
# Order the nodes within the dictionary list so we are aware of tree level
columnTable = defaultdict(list)
queue = deque([(root, 0)])
# While queue not empty, traverse:
while queue:
node, column = queue.popleft()
if node:
columnTable[column].append(node.val)
queue.append((node.left, column - 1))
queue.append((node.right, column + 1))
return [columnTable[x] for x in sorted(columnTable.keys())]
# Time: O(N), Space: O(N)
# Keep track of min, max column so that we do not need to sort and achieve O(N) rather than NLogN
def verticalOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
if root is None:
return []
columnTable = defaultdict(list)
min_column, max_column = 0,0
queue = deque([(root, 0)])
while queue:
node, column = queue.popleft()
if node is not None:
columnTable[column].append(node.val)
min_column = min(min_column, column)
max_column = max(max_column, column)
queue.append((node.left, column - 1))
queue.append((node.right, column + 1))
return [columnTable[x] for x in range(min_column, max_column + 1)]