Find all possible Binary Trees with given Inorder Traversal
Last Updated : 23 Jul, 2025
Given an array that represents Inorder Traversal, the task is to find all possible Binary Trees with the given inorder traversal and print their preorder traversals.
The idea is to generate all possible binary trees that can be formed from a given inorder traversal by recursively selecting each element as the root of the tree. Once a root is chosen, the remaining elements are divided into two subarrays—one for the left subtree and one for the right subtree. We then recursively generate all possible left and right subtrees for that root and combine them to form different binary trees.
Lets's say, for an inorder traversal [1, 2, 3]:
We treat each element (1, 2, or 3) as the root.
For root 2, the left subtree will be [1] and the right subtree will be [3]. Combine the trees formed from [1] and [3] with root 2 to generate one of the possible trees.
Repeat this for every element being the root and recursively generate subtrees.
Below is the implementation of the above approach:
C++
// C++ program to find binary tree with given // inorder traversal#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=nullptr;}};// Function to construct all possible binary trees // with given inorder traversalvector<Node*>getTrees(constvector<int>&inorder,intstart,intend){vector<Node*>trees;// Base case: If start index is greater than end, // return empty tree (nullptr)if(start>end){trees.push_back(nullptr);returntrees;}// Iterate through all values in the array // and construct left and right subtreesfor(inti=start;i<=end;++i){// Generate all left subtreesvector<Node*>leftTrees=getTrees(inorder,start,i-1);// Generate all right subtreesvector<Node*>rightTrees=getTrees(inorder,i+1,end);// Combine each left and right subtree with the current rootfor(Node*left:leftTrees){for(Node*right:rightTrees){// Make inorder[i] the rootNode*root=newNode(inorder[i]);root->left=left;root->right=right;// Add the constructed tree to the list of treestrees.push_back(root);}}}returntrees;}voidpreorder(Node*root){if(root!=nullptr){cout<<root->data<<" ";preorder(root->left);preorder(root->right);}}intmain(){vector<int>inorder={1,2,3};intn=inorder.size();vector<Node*>trees=getTrees(inorder,0,n-1);for(inti=0;i<trees.size();++i){preorder(trees[i]);cout<<"\n";}return0;}
Java
// Java program to find binary tree// with given inorder traversalimportjava.util.ArrayList;importjava.util.List;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// Function to construct all possible binary trees with// given inorder traversalstaticList<Node>getTrees(List<Integer>inorder,intstart,intend){List<Node>trees=newArrayList<>();// Base case: If start index is greater than end,// return empty tree (null)if(start>end){trees.add(null);returntrees;}// Iterate through all values in the array and// construct left and right subtreesfor(inti=start;i<=end;++i){// Generate all left subtreesList<Node>leftTrees=getTrees(inorder,start,i-1);// Generate all right subtreesList<Node>rightTrees=getTrees(inorder,i+1,end);// Combine each left and right subtree with the// current rootfor(Nodeleft:leftTrees){for(Noderight:rightTrees){// Make inorder[i] the rootNoderoot=newNode(inorder.get(i));root.left=left;root.right=right;trees.add(root);}}}returntrees;}// Function to print preorder traversal of the treestaticvoidpreorder(Noderoot){if(root!=null){System.out.print(root.data+" ");preorder(root.left);preorder(root.right);}}publicstaticvoidmain(String[]args){List<Integer>inorder=List.of(1,2,3);intn=inorder.size();List<Node>trees=getTrees(inorder,0,n-1);for(Nodetree:trees){preorder(tree);System.out.println();}}}
Python
# Python program to find binary tree # with given inorder traversalclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=None# Function to construct all possible binary trees # with given inorder traversaldefget_trees(inorder,start,end):trees=[]# Base case: If start index is greater than end,# return empty tree (None)ifstart>end:trees.append(None)returntrees# Iterate through all values in the array # and construct left and right subtreesforiinrange(start,end+1):# Generate all left subtreesleft_trees=get_trees(inorder,start,i-1)# Generate all right subtreesright_trees=get_trees(inorder,i+1,end)# Combine each left and right subtree with the current rootforleftinleft_trees:forrightinright_trees:# Make inorder[i] the rootroot=Node(inorder[i])root.left=leftroot.right=righttrees.append(root)returntreesdefpreorder(root):ifroot:print(root.data,end=" ")preorder(root.left)preorder(root.right)if__name__=="__main__":inorder=[1,2,3]n=len(inorder)trees=get_trees(inorder,0,n-1)fortreeintrees:preorder(tree)print()
C#
// C# program to find binary tree with given inorder// traversalusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// Function to construct all possible binary trees with// given inorder traversalstaticList<Node>GetTrees(List<int>inorder,intstart,intend){List<Node>trees=newList<Node>();// Base case: If start index is greater than end,// return empty tree (null)if(start>end){trees.Add(null);returntrees;}// Iterate through all values in the array and// construct left and right subtreesfor(inti=start;i<=end;++i){// Generate all left subtreesList<Node>leftTrees=GetTrees(inorder,start,i-1);// Generate all right subtreesList<Node>rightTrees=GetTrees(inorder,i+1,end);// Combine each left and right subtree with the// current rootforeach(NodeleftinleftTrees){foreach(NoderightinrightTrees){// Make inorder[i] the rootNoderoot=newNode(inorder[i]);root.left=left;root.right=right;trees.Add(root);}}}returntrees;}staticvoidPreorder(Noderoot){if(root!=null){Console.Write(root.data+" ");Preorder(root.left);Preorder(root.right);}}staticvoidMain(string[]args){List<int>inorder=newList<int>{1,2,3};intn=inorder.Count;List<Node>trees=GetTrees(inorder,0,n-1);foreach(Nodetreeintrees){Preorder(tree);Console.WriteLine();}}}
JavaScript
// JavaScript program to find binary tree with given inorder// traversalclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}// Function to construct all possible binary trees with// given inorder traversalfunctiongetTrees(inorder,start,end){lettrees=[];// Base case: // If start index is greater than end, return// empty tree (null)if(start>end){trees.push(null);returntrees;}// Iterate through all values in the array and construct// left and right subtreesfor(leti=start;i<=end;++i){// Generate all left subtreesletleftTrees=getTrees(inorder,start,i-1);// Generate all right subtreesletrightTrees=getTrees(inorder,i+1,end);// Combine each left and right subtree with the// current rootfor(letleftofleftTrees){for(letrightofrightTrees){// Make inorder[i] the rootletroot=newNode(inorder[i]);root.left=left;root.right=right;trees.push(root);}}}returntrees;}functionpreorder(root){if(root!==null){console.log(root.data+" ");preorder(root.left);preorder(root.right);}}letinorder=[1,2,3];letn=inorder.length;lettrees=getTrees(inorder,0,n-1);for(lettreeoftrees){preorder(tree);console.log("\n");}
Output
1 2 3
1 3 2
2 1 3
3 1 2
3 2 1
Time Complexity: O(n * Cn), where Cn is the Catalan number Auxiliary Space: O(Cn + n), for storing trees and the recursion stack.