-
Notifications
You must be signed in to change notification settings - Fork 0
/
199.二叉树的右视图.py
39 lines (31 loc) · 900 Bytes
/
199.二叉树的右视图.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#
# @lc app=leetcode.cn id=199 lang=python
#
# [199] 二叉树的右视图
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def rightSideView(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
self.result = []
self.check_right_tree(root,0)
return self.result
def check_right_tree(self,root,storey):
if not root:
return
if storey >= len(self.result):
self.result.append(root.val)
if root.right:
self.check_right_tree(root.right,storey+1)
if root.left:
self.check_right_tree(root.left,storey+1)
# @lc code=end