-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199.二叉树的右视图.py
58 lines (55 loc) · 1.29 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#
# @lc app=leetcode.cn id=199 lang=python3
#
# [199] 二叉树的右视图
#
# https://leetcode-cn.com/problems/binary-tree-right-side-view/description/
#
# algorithms
# Medium (63.19%)
# Likes: 139
# Dislikes: 0
# Total Accepted: 18.4K
# Total Submissions: 29K
# Testcase Example: '[1,2,3,null,5,null,4]'
#
# 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
#
# 示例:
#
# 输入: [1,2,3,null,5,null,4]
# 输出: [1, 3, 4]
# 解释:
#
# 1 <---
# / \
# 2 3 <---
# \ \
# 5 4 <---
#
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
# BFS queue
view = []
if not root:
return view
level = [root]
while level:
view.append(level[-1].val)
for i in range(len(level)):
top = level.pop(0)
if top.left:
level.append(top.left)
if top.right:
level.append(top.right)
return view
# @lc code=end