用一个全局数组记录结果
递归时记录深度
优先搜索右子树
当递归到第i层,且当前结果长度<i时,计入这个节点的值
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.res = []
self.length = 0
def rightSideView(self, root: TreeNode) -> List[int]:
if root == None:
return []
self.find_most_right_in_depth_th_layer(root,1)
return self.res
def find_most_right_in_depth_th_layer(self,root,depth):
if root == None:
return 0
if self.length < depth:
self.res.append(root.val)
self.length += 1
self.find_most_right_in_depth_th_layer(root.right,depth + 1)
self.find_most_right_in_depth_th_layer(root.left,depth + 1)