Python simple code


  • 0
    P
    class Solution(object):
        def rightSideView(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            # if root == []:
            # 	return []
            travel = []
            travel.append( (root, 0) )
            res = []
            index = -1
            
            while travel:
                currNode = travel.pop()
                node, level = currNode[0], currNode[1]
                if not node:
                    return res
                if node.left:
                	travel.append( (node.left, level+1) )
                if node.right:
                	travel.append( (node.right, level+1) )
                if level > index:
                	res.append(node.val)
                	index += 1
            return res
    

Log in to reply
 

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