# 二叉树的右视图

<p>给定一个二叉树的 <strong>根节点</strong> <code>root</code>，想象自己站在它的右侧，按照从顶部到底部的顺序，返回从右侧所能看到的节点值。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<p><img src="https://img-service.csdnimg.cn/img_convert/d8e254ac37a1738da124338966297a76.jpeg" style="width: 270px; " /></p>

<pre>
<strong>输入:</strong> [1,2,3,null,5,null,4]
<strong>输出:</strong> [1,3,4]
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong> [1,null,3]
<strong>输出:</strong> [1,3]
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong> []
<strong>输出:</strong> []
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li>二叉树的节点个数的范围是 <code>[0,100]</code></li>
	<li><meta charset="UTF-8" /><code>-100 <= Node.val <= 100</code> </li>
</ul>


## template

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        curnode = [root]
        nexnode = []
        res.append(curnode[0].val)

        while curnode:
            for s in curnode:
                if s.right:
                    nexnode.append(s.right)
                if s.left:
                    nexnode.append(s.left)
            if nexnode:
                res.append(nexnode[0].val)
            curnode = nexnode
            nexnode = []
        return res

```

## 答案

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        curnode = [root]
        nexnode = []
        res.append(curnode[0].val)

        while curnode:
            for s in curnode:
                if s.right:
                    nexnode.append(s.right)
                if s.left:
                    nexnode.append(s.left)
            if nexnode:
                res.append(nexnode[0].val)
            curnode = nexnode
            nexnode = []
        return res
```

## 选项

### A

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        curnode = [root]
        nexnode = []
        res.append(curnode[0].val)

        while curnode:
            for s in curnode:
                if nexnode:
                    res.append(nexnode[0].val)
            if s.right:
                nexnode.append(s.right)
            if s.left:
                nexnode.append(s.left)
            curnode = nexnode
            nexnode = []
        return res
```

### B

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        curnode = [root]
        nexnode = []
        res.append(curnode[0].val)

        while curnode:
            for s in curnode:
                if s.right:
                    nexnode.append(s.right)
                if s.left:
                    nexnode.append(s.left)
            curnode = nexnode
            nexnode = []
        if nexnode:
            res.append(nexnode[0].val)
        return res
```

### C

```python
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def rightSideView(self, root: TreeNode) -> List[int]:
        if not root:
            return []
        res = []
        curnode = [root]
        nexnode = []
        res.append(curnode[0].val)

        while curnode:
            for s in curnode:
                if s.right:
                    nexnode.append(s.right)
                if s.left:
                    nexnode.append(s.left)
                if nexnode:
                    res.append(nexnode[0].val)
                curnode = nexnode
            nexnode = []
        return res
```