42ms Python Level Order Traversal


  • 0
    E
    from collections import deque
    class Solution(object):
        def rightSideView(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root: return [] 
            queue, rList = deque(), []
            queue.append(root)
            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.