199. Binary Tree Right Side View

199. Binary Tree Right Side View

题目:

https://leetcode.com/problems/binary-tree-right-side-view/

难度:

Medium

还是在玩第102题,level order traversal.

class Solution(object):
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None: return []
      
        res = []
        curLevel = [root]
        while curLevel:
            nextLevel = []
            tmpRes = []
            for node in curLevel:
                tmpRes.append(node.val)
                if node.left: nextLevel.append(node.left)
                if node.right: nextLevel.append(node.right)
            res.append(tmpRes[-1])
            curLevel = nextLevel
        return res

你可能感兴趣的:(199. Binary Tree Right Side View)