42ms Python Level Order Traversal

  • 0
    from collections import deque
    class Solution(object):
        def rightSideView(self, root):
            :type root: TreeNode
            :rtype: List[int]
            if not root: return [] 
            queue, rList = deque(), []
            while queue:
                items = len(queue)
                for i in range(items):
                    node = queue.popleft()
                    if i == items-1: rList.append(node.val)
                    if node.left: queue.append(node.left)
                    if node.right: queue.append(node.right)
            return rList

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.