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

# All Possible Full Binary Trees Python Solution

> Tested Python solution for LeetCode 894 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 894, [Medium](/catalog/medium). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming), [Tree](/catalog/topics/tree), [Recursion](/catalog/topics/recursion), [Memoization](/catalog/topics/memoization), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/all-possible-full-binary-trees/description/).

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

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

## Problem

Given an integer \<code>n\</code>, return \<em>a list of all possible \<strong>full binary trees\</strong> with\</em> \<code>n\</code> \<em>nodes\</em>. Each node of each tree in the answer must have \<code>Node.val == 0\</code>.\</p>

\<p>Each element of the answer is the root node of one possible tree. You may return the final list of trees in \<strong>any order\</strong>.\</p>

\<p>A \<strong>full binary tree\</strong> is a binary tree where each node has exactly \<code>0\</code> or \<code>2\</code> children.\</p>

### Examples

```
Input: n = 7
Output: [[0,0,0,None,None,0,0,None,None,0,0],[0,0,0,None,None,0,0,None,None,0,0],[0,0,0,0,0,None,None,0,0],[0,0,0,0,0,None,None,0,0],[0,0,0,0,0,0,0]]
```

```
Input: n = 3
Output: [[0,0,0]]
```

### Constraints

* 1 \<= n \<= 20

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from functools import cache

from leetcode_py import TreeNode


class Solution:
    # Time: O(Catalan(n/2) * n)
    # Space: O(Catalan(n/2) * n)
    def all_possible_fbt(self, n: int) -> list[TreeNode[int] | None]:
        @cache
        def build(count: int) -> list[TreeNode[int] | None]:
            if count == 1:
                return [TreeNode(0)]
            trees: list[TreeNode[int] | None] = []
            for left_count in range(1, count - 1, 2):
                for left in build(left_count):
                    for right in build(count - 1 - left_count):
                        root = TreeNode(0)
                        root.left = left
                        root.right = right
                        trees.append(root)
            return trees

        if n % 2 == 0:
            return []
        return build(n)
```

## Complexity

| Time | Space |
| - | - |
| O(Catalan(n/2) \* n) | O(Catalan(n/2) \* n) |

## Tags

[NeetCode All](/catalog/neetcode).


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