Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. from collections import deque
  9.  
  10. class Solution:
  11. def rightSideView(self, root: TreeNode) -> List[int]:
  12. res = []
  13. if not root: return res
  14.  
  15. q = deque([root])
  16. while q:
  17. cnt = len(q)
  18. last = None
  19. for _ in range(cnt):
  20. last = q.popleft()
  21. if last.left: q.append(last.left)
  22. if last.right: q.append(last.right)
  23. res.append(last.val)
  24.  
  25. return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement