Iam_Sandeep

Bottom view of binary tree

Mar 8th, 2022
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. from collections import defaultdict
  2. class Solution:
  3.     def bottomView(self, root):
  4.         q=deque()
  5.         q.append((root,0))
  6.         d=defaultdict(list)
  7.         while q:
  8.             for _ in range(len(q)):
  9.                 t,v=q.popleft()
  10.                 d[v].append(t.data)
  11.                 if t.left:q.append((t.left,v-1))
  12.                 if t.right:q.append((t.right,v+1))
  13.         ans=[]
  14.         for i in sorted(d.keys()):
  15.             ans.append(d[i][-1])
  16.         return ans
Advertisement
Add Comment
Please, Sign In to add comment