VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-all-possible-n-nodes-full-binary-trees/

⇱ Print all possible N-nodes Full Binary Trees - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all possible N-nodes Full Binary Trees

Last Updated : 23 Jul, 2025

Given an integer n, the task is to find all possible Full Binary Trees with n nodes. The value at the nodes does not contribute to be a criteria for different Full Binary Tree, except for NULL,so take them as 0.

A full binary tree is a binary tree in which every node has exactly 0 or 2 children.

Examples: 

Input:  N = 7
Output: [[0, 0, 0, null, null, 0, 0, null, null, 0, 0, null, null, null, null],
              [0, 0, 0, null, null, 0, 0, 0, 0, null, null, null, null, null, null],
             [0, 0, 0, 0, 0, null, null, null, null, 0, 0, null, null, null, null],
             [0, 0, 0, 0, 0, null, null, 0, 0, null, null, null, null, null, null],
            [0, 0, 0, 0, 0, 0, 0, null, null, null, null, null, null, null, null]]
Explanation: The possible full binary trees are - 

👁 Print-all-possible-N-nodes-Full-Binary-Trees

Approach:

The simplest way to solve the problem is to use recursion using the concept of memoization and check for each subtree if there is a odd number of nodes or not, because a full binary tree has odd nodes.

Below is the implementation of the above approach:


Output
[0, 0, 0, null, null, 0, 0, null, null, 0, 0, null, null, null, null]
[0, 0, 0, null, null, 0, 0, 0, 0, null, null, null, null, null, null]
[0, 0, 0, 0, 0, null, null, null, null, 0, 0, null, null, null, null]
[0, 0, 0, 0, 0, null, null, 0, 0, null, null, null, null, null, null]
[0, 0, 0, 0, 0, 0, 0, null, null, null, null, null, null, null, null]

Time Complexity: O(2^n), where n is number of nodes given.
Auxiliary Space:O(2^n) 

Comment