# -- weird, I think my version is better than NC: I just don't add None nodes class Solution: def rightSideView(self, root: Optional[TreeNode]) -> List[int]: if not root: return [] q = deque([root]) res = [] while q: res.append(q[-1].val) for _ in range(len(q)): node = q.popleft() if node.left: q.append(node.left) if node.right: q.append(node.right) return res # class Solution: NC version def rightSideView(self, root: TreeNode) -> List[int]: res = [] q = collections.deque([root]) while q: rightSide = None qLen = len(q) for i in range(qLen): node = q.popleft() if node: rightSide = node q.append(node.left) q.append(node.right) if rightSide: res.append(rightSide.val) return res
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter