> ## 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.

# Construct Binary Tree from Inorder and

> Tested Python solution for LeetCode 106 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 106, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Tree](/catalog/topics/tree), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/).

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

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

## Problem

Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return *the binary tree*.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)

```
Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output: [3,9,20,null,null,15,7]
```

```
Input: inorder = [-1], postorder = [-1]
Output: [-1]
```

### Constraints

* 1 \<= inorder.length \<= 3000
* postorder.length == inorder.length
* -3000 \<= inorder\[i], postorder\[i] \<= 3000
* inorder and postorder consist of **unique** values
* Each value of postorder also appears in inorder
* inorder is **guaranteed** to be the inorder traversal of the tree
* postorder is **guaranteed** to be the postorder traversal of the tree

## Solution

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

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


class Solution:
    # Time: O(n)
    # Space: O(n)
    def build_tree(self, inorder: list[int], postorder: list[int]) -> TreeNode[int] | None:
        indices: dict[int, int] = {value: i for i, value in enumerate(inorder)}

        def build(
            in_left: int, in_right: int, post_left: int, post_right: int
        ) -> TreeNode[int] | None:
            if in_left > in_right:
                return None
            root_value = postorder[post_right]
            root = TreeNode[int](root_value)
            mid = indices[root_value]
            left_size = mid - in_left
            root.left = build(in_left, mid - 1, post_left, post_left + left_size - 1)
            root.right = build(mid + 1, in_right, post_left + left_size, post_right - 1)
            return root

        return build(0, len(inorder) - 1, 0, len(postorder) - 1)
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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