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:

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]]

Example 2:

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

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

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<TreeNode *> createFBT(int n)
    {

        vector<TreeNode *> v;

        if (n == 1)
        {
            TreeNode *t = new TreeNode(0);
            v.push_back(t);
            return v;
        }

        if (n % 2 != 0)
        {
            for (int i = 1; i <= n - 2; i += 2)
            {
                vector<TreeNode *> lt = createFBT(i);
                vector<TreeNode *> rt = createFBT(n - i - 1);

                for (int i = 0; i < lt.size(); i++)
                {
                    for (int j = 0; j < rt.size(); j++)
                    {

                        TreeNode *t = new TreeNode(0);
                        t->left = lt[i];
                        t->right = rt[j];
                        v.push_back(t);
                    }
                }
            }
        }

        return v;
    }

    vector<TreeNode *> allPossibleFBT(int N)
    {
        vector<TreeNode *> res = createFBT(N);
        return res;
    }
};

Time Complexity: O(2 ^ n)

Solution: (Optimized DP)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    unordered_map<int, vector<TreeNode *>> m;
    vector<TreeNode *> createFBT(int n)
    {

        if (m.find(n) != m.end())
        {
            return m[n];
        }

        vector<TreeNode *> v;

        if (n == 1)
        {
            TreeNode *t = new TreeNode(0);
            v.push_back(t);
            return v;
        }

        if (n % 2 != 0)
        {
            for (int i = 1; i <= n - 2; i += 2)
            {
                vector<TreeNode *> lt = createFBT(i);
                vector<TreeNode *> rt = createFBT(n - i - 1);

                for (int i = 0; i < lt.size(); i++)
                {
                    for (int j = 0; j < rt.size(); j++)
                    {

                        TreeNode *t = new TreeNode(0);
                        t->left = lt[i];
                        t->right = rt[j];
                        v.push_back(t);
                    }
                }
            }
        }
        m[n] = v;
        return v;
    }

    vector<TreeNode *> allPossibleFBT(int N)
    {
        vector<TreeNode *> res = createFBT(N);
        return res;
    }
};

Last updated