Simply tree level traversal, Python


  • 0
    2
    class Solution(object):
        def rightSideView(self, root):
            view = []
            if not root:
                return view
            
            queue = []
            queue.append((root, 1))
            
            while queue:
                node, level = queue.pop(0)
                if level > len(view):
                    view.append(node.val)
                
                if node.right:
                    queue.append((node.right, level+1))
                    
                if node.left:
                    queue.append((node.left, level+1))
            
            return view
    

Log in to reply
 

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