> ## 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 Upside Down Python Solution

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

LeetCode 156, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Depth-First Search](/catalog/topics/depth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/binary-tree-upside-down/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 156   # by problem number
lcpy gen -s binary_tree_upside_down   # by problem name
```

## Problem

Given the `root` of a binary tree, turn the tree upside down and return the new root.

You can turn a binary tree upside down with the following steps:

1. The original left child becomes the new root.
2. The original root becomes the new right child.
3. The original right child becomes the new left child.

The mentioned steps are done level by level. It is guaranteed that every right node has a sibling (a left node that shares the same parent) and has no children.

### Examples

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

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

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

### Constraints

* The number of nodes in the tree is in the range \[0, 10]
* 1 \<= Node.val \<= 10
* Every right node in the tree has a sibling (a left node that shares the same parent)
* Every right node in the tree has no children

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def upside_down_binary_tree(self, root: TreeNode[int] | None) -> TreeNode[int] | None:
        curr = root
        prev: TreeNode[int] | None = None
        prev_right: TreeNode[int] | None = None
        while curr is not None:
            next_curr = curr.left
            orig_right = curr.right
            curr.left = prev_right
            curr.right = prev
            prev_right = orig_right
            prev = curr
            curr = next_curr
        return prev
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.