> For the complete documentation index, see [llms.txt](https://liuyang89116.gitbook.io/my-leetcode-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_tree_bfs-_dfs/problem_199_binary_tree_right_side_view.md).

# Problem 199: Binary Tree Right Side View

> <https://leetcode.com/problems/binary-tree-right-side-view/>

## 思路

* 这道题其实就是在 BFS 遍历的基础上稍加改变：只存每个 level 最有边的那个数
* 怎么实现这个过程呢？ 可以用 currIndex 标记当前的 node 的 index;用 nextIndex 标记下一个 node 的index
* 每次初始的时候，currIndex > 0， nextIndex = 0。这样做是因为每一个 level 上会有很多 node，当 currIndex 为 0 的时候，我们知道这是最后一个 node 了，可以存他了。&#x20;
* nextIndex 用来记下下一个 level 里有多少个 nodes，方便给下一次的 currIndex 赋值

```java
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> rst = new ArrayList<Integer>();
        if (root == null) {
            return rst;
        }

        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int currIndex = 1, nextIndex = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode currNode = queue.poll();
                currIndex--;
                if (currIndex == 0) {
                    rst.add(currNode.val);
                }
                if (currNode.left != null) {
                    queue.offer(currNode.left);
                    nextIndex++;
                }
                if (currNode.right != null) {
                    queue.offer(currNode.right);
                    nextIndex++;
                }
                if (currIndex == 0) {
                    currIndex = nextIndex;
                    nextIndex = 0;
                }
            }
        }

        return rst;
    }
}
```

## 易错点

1. 如果是求 left side view 呢？

   ```java
   if (nextIndex == 0) {
       rst.add(currNode.val);
   }
   ```

   只需要做这一个改动。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://liuyang89116.gitbook.io/my-leetcode-book/chapter_3_binary_tree_bfs-_dfs/problem_199_binary_tree_right_side_view.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
