python 35ms


  • 0
    V
    class Solution(object):
        def rightSideView(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root:
                return []
            
            res = []
            queue = [root]
            
            while queue:
                tempQueue = []
                last = None
                for node in queue:
                    last = node.val
                    if node.left:
                        tempQueue.append(node.left)
                    if node.right:
                        tempQueue.append(node.right)
                
                res.append(last)
                queue = tempQueue
                
            return res
    

Log in to reply
 

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