Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def topView(self,root):
- ans=deque()
- ans.appendleft(root.data)
- q=deque()
- q.append((root,0))#adding verticle line height in vertical order traversal
- l,r=0,0
- while q:
- t,d=q.popleft()
- if d<l:#if you have found left most vert line then add to left of deque
- l=d
- ans.appendleft(t.data)
- if d>r:#if you have found right most vert line then add to right of deque
- r=d
- ans.append(t.data)
- if t.left:q.append((t.left,d-1))
- if t.right:q.append((t.right,d+1))
- return ans
Advertisement
Add Comment
Please, Sign In to add comment