199. Binary Tree Right Side View

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def rightSideView(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
       
        return self.recursion(root,[],1)
        
    def recursion(self,root,result,depth):
        if not root:
            return result 
        
        if depth>len(result):
            result.append(root.val)
        
        self.recursion(root.right,result,depth+1)
        self.recursion(root.left,result,depth+1)
        return result 

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