-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm314.py
25 lines (21 loc) · 809 Bytes
/
m314.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
# 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:
def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
output = {}
toVisit = deque([(0, root)]) # (col, node)
while toVisit :
col, currNode = toVisit.popleft()
if not currNode :
continue
if col in output :
output[col].append(currNode.val)
else :
output[col] = [currNode.val]
toVisit.append((col + 1, currNode.left))
toVisit.append((col - 1, currNode.right))
return [output.get(x) for x in sorted(output.keys(), reverse=True)]