# 199. 二叉树的右视图

[https://leetcode-cn.com/problems/binary-tree-right-side-view/](https://leetcode-cn.com/problems/binary-tree-right-side-view/)

## 解法一：层序

每层按从右到左顺序访问，然后记录每层第一个节点即可。

若求**左视图**，则反之。

```python
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import queue
class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        q = queue.Queue()
        q.put(root)
        while q.qsize() > 0:
            n = q.qsize()
            for i in range(n):
                node = q.get()
                if i == 0:  #记录最右节点
                    res.append(node.val)
                if node.right:
                    q.put(node.right)
                if node.left:
                    q.put(node.left)
        return res
```
