> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Binary Tree Right Side View Python Solution

> Tested Python solution for LeetCode 199 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 199, Medium. Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree. [View on LeetCode](https://leetcode.com/problems/binary-tree-right-side-view/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 199   # by problem number
lcpy gen -s binary_tree_right_side_view   # by problem name
```

## Problem

Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return *the values of the nodes you can see ordered from top to bottom*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png)

```
Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]
```

![Example 2](https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png)

```
Input: root = [1,2,3,4,null,null,null,5]
Output: [1,3,4,5]
```

```
Input: root = [1,null,3]
Output: [1,3]
```

```
Input: root = []
Output: []
```

### Constraints

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_right_side_view/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/binary_tree_right_side_view/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import deque

from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(h)
    def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
        result: list[int] = []

        def dfs(node: TreeNode[int] | None, level: int) -> None:
            if not node:
                return
            if level == len(result):
                result.append(node.val)
            dfs(node.right, level + 1)
            dfs(node.left, level + 1)

        dfs(root, 0)
        return result


class SolutionDFS:
    # Time: O(n)
    # Space: O(h)
    def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
        if not root:
            return []

        result: list[int] = []
        stack = [(root, 0)]

        while stack:
            node, level = stack.pop()
            if level == len(result):
                result.append(node.val)
            if node.left:
                stack.append((node.left, level + 1))
            if node.right:
                stack.append((node.right, level + 1))

        return result


class SolutionBFS:
    # Time: O(n)
    # Space: O(w)
    def right_side_view(self, root: TreeNode[int] | None) -> list[int]:
        if not root:
            return []

        result: list[int] = []
        queue = deque([root])

        while queue:
            level_size = len(queue)
            for i in range(level_size):
                node = queue.popleft()
                if i == level_size - 1:  # rightmost node
                    result.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)

        return result
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(h)  |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
