All Possible Full Binary Trees - Problem
All Possible Full Binary Trees
Given an integer
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
• Each tree must have exactly
• 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.
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
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
✓ Linear Growth
Space Complexity
O(4^n / √n)
Space for memoization table plus space to store all generated trees
⚡ 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)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code