添加链接描述
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:def rightSideView(self, root: Optional[TreeNode]) -> List[int]:# 思路是层序遍历,把队列中最后一个元素加入结果集中reslut=[]if not root:return reslutque=collections.deque()que.append(root)while que:size=len(que)while size:cur=que.popleft()if cur.left:que.append(cur.left)if cur.right:que.append(cur.right)if size==1:reslut.append(cur.val)size=size-1return reslut
思路:
- 二叉树的右视图并不是单纯的右子树
- 而是应该先层序遍历,把每一层的最后一个元素放进result数组中
- 简单题