Similar Easy Python with LevelOrder, LevelOrderBottom, ZigZagLevelOrder


  • 0
    W
    class Solution(object):
        def rightSideView(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if root==None:
                return []
            q,re=[],[]
            height,keep_height,x=1,1,root.val
            q.append((root,1))
            while q:
                node,height=q.pop(0)
                print node.val,height, keep_height
                if height==keep_height+1:
                    re.append(x)
                    x=node.val
                elif height==keep_height:
                    x=node.val
                keep_height=height
                if node.left:
                    q.append((node.left,height+1))
                if node.right:
                    q.append((node.right,height+1))
            re.append(x)
            return re
    

    Introducing height for each node, so only output the last element for each level


Log in to reply
 

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