Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def verticalOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
- if not root:
- return []
- from collections import defaultdict, deque
- q = deque()
- q.append((root, 0, 0))
- hashmap = defaultdict(list)
- min_col = float("inf")
- max_col = -float("inf")
- while q:
- node, col, level = q.popleft()
- if node: # check if the node is not None
- if col < min_col:
- min_col = col
- if col > max_col:
- max_col = col
- hashmap[col].append(node.val)
- q.append((node.left, col - 1, level + 1))
- q.append((node.right, col + 1, level + 1))
- return [hashmap[col] for col in range(min_col, max_col + 1)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement