> ## 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 Preorder and (#889)

> Tested Python solution for LeetCode 889 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 889, [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-preorder-and-postorder-traversal/description/).

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

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

## Problem

Given two integer arrays, \<code>preorder\</code> and \<code>postorder\</code> where \<code>preorder\</code> is the preorder traversal of a binary tree of \<strong>distinct\</strong> values and \<code>postorder\</code> is the postorder traversal of the same tree, reconstruct and return the binary tree.\</p>

\<p>If there exist multiple answers, you can return \<strong>any\</strong> of them.\</p>

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/07/24/lc-prepost.jpg)

```
Input: preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
Output: [1,2,3,4,5,6,7]
```

```
Input: preorder = [1], postorder = [1]
Output: [1]
```

### Constraints

* 1 \<= preorder.length \<= 30
* 1 \<= preorder\[i] \<= preorder.length
* All the values of preorder are unique.
* postorder.length == preorder.length
* 1 \<= postorder\[i] \<= postorder.length
* All the values of postorder are unique.
* It is guaranteed that preorder and postorder are the preorder traversal and postorder traversal of the same binary tree.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_binary_tree_from_preorder_and_postorder_traversal/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_binary_tree_from_preorder_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 construct_from_pre_post(
        self, preorder: list[int], postorder: list[int]
    ) -> TreeNode[int] | None:
        index = {value: i for i, value in enumerate(postorder)}
        self._pre_index = 0
        return self._build(preorder, index, 0, len(postorder) - 1)

    def _build(
        self, preorder: list[int], index: dict[int, int], lo: int, hi: int
    ) -> TreeNode[int] | None:
        if lo > hi:
            return None
        node = TreeNode(preorder[self._pre_index])
        self._pre_index += 1
        if lo < hi:
            left_size = index[preorder[self._pre_index]] - lo + 1
            node.left = self._build(preorder, index, lo, lo + left_size - 1)
            node.right = self._build(preorder, index, lo + left_size, hi - 1)
        return node
```

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