All Possible Full Binary Trees - Problem
All Possible Full Binary Trees

Given an integer n, generate and return a list of all possible full binary trees with exactly n nodes. Each node in every tree should have a value of 0.

A full binary tree is a binary tree where every node has either exactly 0 children (leaf node) or exactly 2 children (internal node). No node can have just 1 child.

Key Points:
• All nodes must have Node.val = 0
• Each tree must have exactly n nodes
• Every node has 0 or 2 children (never 1)
• Return trees can be in any order

This problem combines tree construction, recursion, and dynamic programming to efficiently generate all valid tree structures.

Input & Output

example_1.py — Small tree
$ Input: n = 7
Output: [[0,0,0,null,null,0,0,null,null,0,0],[0,0,0,null,null,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,null,null,null,null,0,0],[0,0,0,0,0,null,null,0,0]]
💡 Note: For n=7, there are 5 different full binary trees possible. Each tree has exactly 7 nodes, and every internal node has exactly 2 children.
example_2.py — Single node
$ Input: n = 1
Output: [[0]]
💡 Note: With only 1 node, there's exactly one possible tree: a single node with value 0.
example_3.py — Even number
$ Input: n = 4
Output: []
💡 Note: No full binary tree can have an even number of nodes. This is because each internal node adds exactly 2 children, so the total count is always 1 + 2k = odd.

Visualization

Tap to expand
Full Binary Tree Construction (n=5)0Root (1 node)00Left: 1 nodeRight: 3 nodes3-node subtreeAlternative ArrangementLeft: 3 nodesRight: 1 node
Understanding the Visualization
1
Check if Possible
Full binary trees can only have odd number of nodes (1, 3, 5, 7, ...)
2
Base Case
For n=1, return single node tree with value 0
3
Split Strategy
For n nodes, try all ways to split into i left nodes and (n-1-i) right nodes
4
Recursive Building
Get all possible left subtrees and right subtrees recursively
5
Combination
Combine each left subtree with each right subtree to form complete trees
6
Memoization
Store results to avoid recalculating same subproblems
Key Takeaway
🎯 Key Insight: Full binary trees follow a recursive pattern where each tree can be built by choosing a root and recursively constructing all possible left and right subtrees, with memoization preventing redundant work.

Time & Space Complexity

Time Complexity
⏱️
O(4^n / √n)

This is the nth Catalan number complexity - each unique subproblem is solved only once

n
2n
Linear Growth
Space Complexity
O(4^n / √n)

Space for memoization table plus space to store all generated trees

n
2n
Linearithmic Space

Constraints

  • 1 ≤ n ≤ 20
  • n is a positive integer
  • All nodes in returned trees must have val = 0
  • Each tree must be a full binary tree (0 or 2 children per node)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
87.6K Views
Medium Frequency
~25 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen