32. All Possible Full Binary Trees

Given an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.

Each element of the answer is the root node of one possible tree. You may return the final list of trees in any order.

A full binary tree is a binary tree where each node has exactly 0 or 2 children.

Example 1:

Example 2:

Solution: (Recursion)

Approach: We can create a FULL Binary Tree when we have odd no of nodes Thus, for N N≥3, we can formulate the recursion: FBT(N)= [All trees with left child from FBT(x) and right child from FBT(N−1−x), for all x].

Time Complexity: O(2 ^ n)

Solution: (Optimized DP)

Last updated

Was this helpful?