Construct a Binary Tree from Postorder and Inorder
Last Updated :
24 Feb, 2023
Given Postorder and Inorder traversals, construct the tree.
Examples:
Input:
in[] = {2, 1, 3}
post[] = {2, 3, 1}
Output: Root of below tree
1
/ \
2 3
Input:
in[] = {4, 8, 2, 5, 1, 6, 3, 7}
post[] = {8, 4, 5, 2, 6, 7, 3, 1}
Output: Root of below tree
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Approach: To solve the problem follow the below idea:
Note: We have already discussed the construction of trees from Inorder and Preorder traversals:
Follow the below steps:
Let us see the process of constructing a tree from in[] = {4, 8, 2, 5, 1, 6, 3, 7} and post[] = {8, 4, 5, 2, 6, 7, 3, 1}:
- We first find the last node in post[]. The last node is “1”, we know this value is the root as the root always appears at the end of postorder traversal.
- We search “1” in in[] to find the left and right subtrees of the root. Everything on the left of “1” in in[] is in the left subtree and everything on right is in the right subtree.
1
/ \
[4, 8, 2, 5] [6, 3, 7]
- We recur the above process for the following two.
- Recur for in[] = {6, 3, 7} and post[] = {6, 7, 3} Make the created tree as right child of root.
- Recur for in[] = {4, 8, 2, 5} and post[] = {8, 4, 5, 2}. Make the created tree the left child of the root.
Note: One important observation is, that we recursively call for the right subtree before the left subtree as we decrease the index of the postorder index whenever we create a new node
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data);
int search( int arr[], int strt, int end, int value);
Node* buildUtil( int in[], int post[], int inStrt, int inEnd,
int * pIndex)
{
if (inStrt > inEnd)
return NULL;
Node* node = newNode(post[*pIndex]);
(*pIndex)--;
if (inStrt == inEnd)
return node;
int iIndex = search(in, inStrt, inEnd, node->data);
node->right
= buildUtil(in, post, iIndex + 1, inEnd, pIndex);
node->left
= buildUtil(in, post, inStrt, iIndex - 1, pIndex);
return node;
}
Node* buildTree( int in[], int post[], int n)
{
int pIndex = n - 1;
return buildUtil(in, post, 0, n - 1, &pIndex);
}
int search( int arr[], int strt, int end, int value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break ;
}
return i;
}
Node* newNode( int data)
{
Node* node = (Node*) malloc ( sizeof (Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
void preOrder(Node* node)
{
if (node == NULL)
return ;
printf ( "%d " , node->data);
preOrder(node->left);
preOrder(node->right);
}
int main()
{
int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = sizeof (in) / sizeof (in[0]);
Node* root = buildTree(in, post, n);
cout << "Preorder of the constructed tree : \n" ;
preOrder(root);
return 0;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
class BinaryTree {
Node buildUtil( int in[], int post[], int inStrt,
int inEnd, int postStrt, int postEnd)
{
if (inStrt > inEnd)
return null ;
Node node = new Node(post[postEnd]);
if (inStrt == inEnd)
return node;
int iIndex = search(in, inStrt, inEnd, node.data);
node.left = buildUtil(
in, post, inStrt, iIndex - 1 , postStrt,
postStrt - inStrt + iIndex - 1 );
node.right = buildUtil(in, post, iIndex + 1 , inEnd,
postEnd - inEnd + iIndex,
postEnd - 1 );
return node;
}
int search( int arr[], int strt, int end, int value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break ;
}
return i;
}
void preOrder(Node node)
{
if (node == null )
return ;
System.out.print(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
int in[] = new int [] { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 };
int post[] = new int [] { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 };
int n = in.length;
Node root
= tree.buildUtil(in, post, 0 , n - 1 , 0 , n - 1 );
System.out.println(
"Preorder of the constructed tree : " );
tree.preOrder(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def buildUtil(In, post, inStrt, inEnd, pIndex):
if (inStrt > inEnd):
return None
node = newNode(post[pIndex[ 0 ]])
pIndex[ 0 ] - = 1
if (inStrt = = inEnd):
return node
iIndex = search(In, inStrt, inEnd, node.data)
node.right = buildUtil(In, post, iIndex + 1 ,
inEnd, pIndex)
node.left = buildUtil(In, post, inStrt,
iIndex - 1 , pIndex)
return node
def buildTree(In, post, n):
pIndex = [n - 1 ]
return buildUtil(In, post, 0 , n - 1 , pIndex)
def search(arr, strt, end, value):
i = 0
for i in range (strt, end + 1 ):
if (arr[i] = = value):
break
return i
def preOrder(node):
if node = = None :
return
print (node.data, end = " " )
preOrder(node.left)
preOrder(node.right)
if __name__ = = '__main__' :
In = [ 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 ]
post = [ 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ]
n = len (In)
root = buildTree(In, post, n)
print ( "Preorder of the constructed tree :" )
preOrder(root)
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int data)
{
this .data = data;
left = right = null ;
}
}
public class Index {
public int index;
}
class GFG {
public virtual Node buildUtil( int [] @ in , int [] post,
int inStrt, int inEnd,
Index pIndex)
{
if (inStrt > inEnd) {
return null ;
}
Node node = new Node(post[pIndex.index]);
(pIndex.index)--;
if (inStrt == inEnd) {
return node;
}
int iIndex = search(@ in , inStrt, inEnd, node.data);
node.right = buildUtil(@ in , post, iIndex + 1, inEnd,
pIndex);
node.left = buildUtil(@ in , post, inStrt, iIndex - 1,
pIndex);
return node;
}
public virtual Node buildTree( int [] @ in , int [] post,
int n)
{
Index pIndex = new Index();
pIndex.index = n - 1;
return buildUtil(@ in , post, 0, n - 1, pIndex);
}
public virtual int search( int [] arr, int strt, int end,
int value)
{
int i;
for (i = strt; i <= end; i++) {
if (arr[i] == value) {
break ;
}
}
return i;
}
public virtual void preOrder(Node node)
{
if (node == null ) {
return ;
}
Console.Write(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
public static void Main( string [] args)
{
GFG tree = new GFG();
int [] @ in = new int [] { 4, 8, 2, 5, 1, 6, 3, 7 };
int [] post = new int [] { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = @ in .Length;
Node root = tree.buildTree(@ in , post, n);
Console.WriteLine(
"Preorder of the constructed tree : " );
tree.preOrder(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = this .right = null ;
}
}
function buildUtil(In, post, inStrt, inEnd, postStrt, postEnd)
{
if (inStrt > inEnd)
return null ;
let node = new Node(post[postEnd]);
if (inStrt == inEnd)
return node;
let iIndex = search(In, inStrt, inEnd, node.data);
node.left = buildUtil(
In, post, inStrt, iIndex - 1, postStrt,
postStrt - inStrt + iIndex - 1);
node.right = buildUtil(In, post, iIndex + 1, inEnd,
postEnd - inEnd + iIndex,
postEnd - 1);
return node;
}
function search(arr,strt,end,value)
{
let i;
for (i = strt; i <= end; i++) {
if (arr[i] == value)
break ;
}
return i;
}
function preOrder(node)
{
if (node == null )
return ;
document.write(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
let In=[4, 8, 2, 5, 1, 6, 3, 7];
let post=[8, 4, 5, 2, 6, 7, 3, 1];
let n = In.length;
let root
= buildUtil(In, post, 0, n - 1, 0, n - 1);
document.write(
"Preorder of the constructed tree : <br>" );
preOrder(root);
</script>
|
Output
Preorder of the constructed tree :
1 2 4 8 5 3 6 7
Time Complexity: O(N2), Where N is the length of the given inorder array
Auxiliary Space: O(N), for recursive call stack
Construct a Binary Tree from Postorder and Inorder using hashing:
To solve the problem follow the below idea:
We can optimize the above solution using hashing. We store indexes of inorder traversal in a hash table. So that search can be done O(1) time If given that element in the tree is not repeated.
Follow the below steps to solve the problem:
- We first find the last node in post[]. The last node is “1”, we know this value is the root as the root always appears at the end of postorder traversal.
- we get the index of postorder[i], in inorder using the map to find the left and right subtrees of the root. Everything on the left of “1” in in[] is in the left subtree and everything on right is in the right subtree.
- We recur the above process for the following two.
- Recur for in[] = {6, 3, 7} and post[] = {6, 7, 3} Make the created tree as right child of root.
- Recur for in[] = {4, 8, 2, 5} and post[] = {8, 4, 5, 2}. Make the created tree the left child of the root.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data);
Node* buildUtil( int in[], int post[], int inStrt, int inEnd,
int * pIndex, unordered_map< int , int >& mp)
{
if (inStrt > inEnd)
return NULL;
int curr = post[*pIndex];
Node* node = newNode(curr);
(*pIndex)--;
if (inStrt == inEnd)
return node;
int iIndex = mp[curr];
node->right = buildUtil(in, post, iIndex + 1, inEnd,
pIndex, mp);
node->left = buildUtil(in, post, inStrt, iIndex - 1,
pIndex, mp);
return node;
}
struct Node* buildTree( int in[], int post[], int len)
{
unordered_map< int , int > mp;
for ( int i = 0; i < len; i++)
mp[in[i]] = i;
int index = len - 1;
return buildUtil(in, post, 0, len - 1, &index, mp);
}
Node* newNode( int data)
{
Node* node = (Node*) malloc ( sizeof (Node));
node->data = data;
node->left = node->right = NULL;
return (node);
}
void preOrder(Node* node)
{
if (node == NULL)
return ;
printf ( "%d " , node->data);
preOrder(node->left);
preOrder(node->right);
}
int main()
{
int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = sizeof (in) / sizeof (in[0]);
Node* root = buildTree(in, post, n);
cout << "Preorder of the constructed tree : \n" ;
preOrder(root);
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int data;
Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static Node buildUtil( int in[], int post[], int inStrt,
int inEnd)
{
if (inStrt > inEnd)
return null ;
int curr = post[index];
Node node = newNode(curr);
(index)--;
if (inStrt == inEnd)
return node;
int iIndex = mp.get(curr);
node.right = buildUtil(in, post, iIndex + 1 , inEnd);
node.left = buildUtil(in, post, inStrt, iIndex - 1 );
return node;
}
static HashMap<Integer, Integer> mp
= new HashMap<Integer, Integer>();
static int index;
static Node buildTree( int in[], int post[], int len)
{
for ( int i = 0 ; i < len; i++)
mp.put(in[i], i);
index = len - 1 ;
return buildUtil(in, post, 0 , len - 1 );
}
static void preOrder(Node node)
{
if (node == null )
return ;
System.out.printf( "%d " , node.data);
preOrder(node.left);
preOrder(node.right);
}
public static void main(String[] args)
{
int in[] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 };
int post[] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 };
int n = in.length;
Node root = buildTree(in, post, n);
System.out.print(
"Preorder of the constructed tree : \n" );
preOrder(root);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def buildUtil(inn, post, innStrt, innEnd):
global mp, index
if (innStrt > innEnd):
return None
curr = post[index]
node = Node(curr)
index - = 1
if (innStrt = = innEnd):
return node
iIndex = mp[curr]
node.right = buildUtil(inn, post,
iIndex + 1 , innEnd)
node.left = buildUtil(inn, post, innStrt,
iIndex - 1 )
return node
def buildTree(inn, post, lenn):
global index
for i in range (lenn):
mp[inn[i]] = i
index = lenn - 1
return buildUtil(inn, post, 0 , lenn - 1 )
def preOrder(node):
if (node = = None ):
return
print (node.data, end = " " )
preOrder(node.left)
preOrder(node.right)
if __name__ = = '__main__' :
inn = [ 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 ]
post = [ 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ]
n = len (inn)
mp, index = {}, 0
root = buildTree(inn, post, n)
print ( "Preorder of the constructed tree :" )
preOrder(root)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public
class Node {
public
int data;
public
Node left,
right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static Node buildUtil( int [] init, int [] post,
int inStrt, int inEnd)
{
if (inStrt > inEnd)
return null ;
int curr = post[index];
Node node = newNode(curr);
(index)--;
if (inStrt == inEnd)
return node;
int iIndex = mp[curr];
node.right
= buildUtil(init, post, iIndex + 1, inEnd);
node.left
= buildUtil(init, post, inStrt, iIndex - 1);
return node;
}
static Dictionary< int , int > mp
= new Dictionary< int , int >();
static int index;
static Node buildTree( int [] init, int [] post, int len)
{
for ( int i = 0; i < len; i++)
mp.Add(init[i], i);
index = len - 1;
return buildUtil(init, post, 0, len - 1);
}
static void preOrder(Node node)
{
if (node == null )
return ;
Console.Write(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
public static void Main(String[] args)
{
int [] init = { 4, 8, 2, 5, 1, 6, 3, 7 };
int [] post = { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = init.Length;
Node root = buildTree(init, post, n);
Console.Write(
"Preorder of the constructed tree : \n" );
preOrder(root);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function newNode(data) {
var node = new Node();
node.data = data;
node.left = node.right = null ;
return node;
}
function buildUtil(init, post, inStrt, inEnd) {
if (inStrt > inEnd) {
return null ;
}
var curr = post[index];
var node = newNode(curr);
index--;
if (inStrt == inEnd) {
return node;
}
var iIndex = mp[curr];
node.right = buildUtil(init, post, iIndex + 1, inEnd);
node.left = buildUtil(init, post, inStrt, iIndex - 1);
return node;
}
var mp = {};
var index;
function buildTree(init, post, len) {
for ( var i = 0; i < len; i++) {
mp[init[i]] = i;
}
index = len - 1;
return buildUtil(init, post, 0, len - 1);
}
function preOrder(node) {
if (node == null ) {
return ;
}
document.write(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
var init = [4, 8, 2, 5, 1, 6, 3, 7];
var post = [8, 4, 5, 2, 6, 7, 3, 1];
var n = init.length;
var root = buildTree(init, post, n);
document.write( "Preorder of the constructed tree : <br>" );
preOrder(root);
</script>
|
Output
Preorder of the constructed tree :
1 2 4 8 5 3 6 7
Time Complexity: O(N)
Auxiliary Space: O(N), The extra space is used due to the recursion call stack and to store the elements in the map.
Construct a Binary Tree from Postorder and Inorder using stack and set:
We can use the stack and set without using recursion.
Follow the below steps to solve the problem:
- Create a stack and a set of type Node* and initialize an integer postIndex with N-1
- Run a for loop with p and i, from n-1 to 0
- Create a new Node with value as postorder[p] and set it as the root node, if it is the first node of our newly created tree
- Check if the value of stack top is already present in the set, then remove it from the set and set the left child of stack top equal to the new node and pop out the stack top
- Push the current node into the stack
- Perform step numbers 3,4 and 5 while p is greater than or equal to zero and postorder[p] is not equal to inorder[i]
- Set the new node equal to null and while the stack’s top data is equal to the inorder[i], set the node equal to stack top and pop out the stack top
- If the node is not null then insert the node into the set and push it into the stack also
- Return root of the newly created tree
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
Node( int x)
{
data = x;
left = right = NULL;
}
};
Node* buildTree( int in[], int post[], int n)
{
stack<Node*> st;
set<Node*> s;
int postIndex = n - 1;
Node* root = NULL;
for ( int p = n - 1, i = n - 1; p >= 0;) {
Node* node = NULL;
do {
node = new Node(post[p]);
if (root == NULL) {
root = node;
}
if (st.size() > 0) {
if (s.find(st.top()) != s.end()) {
s.erase(st.top());
st.top()->left = node;
st.pop();
}
else {
st.top()->right = node;
}
}
st.push(node);
} while (post[p--] != in[i] && p >= 0);
node = NULL;
while (st.size() > 0 && i >= 0
&& st.top()->data == in[i]) {
node = st.top();
st.pop();
i--;
}
if (node != NULL) {
s.insert(node);
st.push(node);
}
}
return root;
}
void preOrder(Node* node)
{
if (node == NULL)
return ;
printf ( "%d " , node->data);
preOrder(node->left);
preOrder(node->right);
}
int main()
{
int in[] = { 4, 8, 2, 5, 1, 6, 3, 7 };
int post[] = { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = sizeof (in) / sizeof (in[0]);
Node* root = buildTree(in, post, n);
cout << "Preorder of the constructed tree : \n" ;
preOrder(root);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static class Node {
int data;
Node left, right;
Node( int x)
{
data = x;
left = right = null ;
}
}
static Node buildTree( int in[], int post[], int n)
{
Stack<Node> st = new Stack<>();
HashSet<Node> s = new HashSet<>();
int postIndex = n - 1 ;
Node root = null ;
for ( int p = n - 1 , i = n - 1 ; p >= 0 😉 {
Node node = null ;
do {
node = new Node(post[p]);
if (root == null ) {
root = node;
}
if (st.size() > 0 ) {
if (s.contains(st.peek())) {
s.remove(st.peek());
st.peek().left = node;
st.pop();
}
else {
st.peek().right = node;
}
}
st.push(node);
} while (post[p--] != in[i] && p >= 0 );
node = null ;
while (st.size() > 0 && i >= 0
&& st.peek().data == in[i]) {
node = st.peek();
st.pop();
i--;
}
if (node != null ) {
s.add(node);
st.push(node);
}
}
return root;
}
static void preOrder(Node node)
{
if (node == null )
return ;
System.out.printf( "%d " , node.data);
preOrder(node.left);
preOrder(node.right);
}
public static void main(String[] args)
{
int in[] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 };
int post[] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 };
int n = in.length;
Node root = buildTree(in, post, n);
System.out.print(
"Preorder of the constructed tree : \n" );
preOrder(root);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def buildTree(inorder, post, n):
st = []
set = []
postIndex = n - 1
root = None
p = n - 1
i = n - 1
while p > = 0 :
node = None
while True :
node = Node(post[p])
if root = = None :
root = node
if len (st) > 0 :
if st[ - 1 ] in set :
set .remove(st[ - 1 ])
st[ - 1 ].left = node
st.pop()
else :
st[ - 1 ].right = node
st.append(node)
p - = 1
if post[p + 1 ] = = inorder[i] or p < 0 :
break
node = None
while len (st) > 0 and i > = 0 and st[ - 1 ].data = = inorder[i]:
node = st[ - 1 ]
st.pop()
i - = 1
if node ! = None :
set .append(node)
st.append(node)
return root
def preOrder(node):
if node = = None :
return
print (node.data, end = " " )
preOrder(node.left)
preOrder(node.right)
if __name__ = = '__main__' :
inorder = [ 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 ]
post = [ 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ]
n = len (inorder)
root = buildTree(inorder, post, n)
print ( "Preorder of the constructed tree :" )
preOrder(root)
|
C#
using System;
using System.Collections.Generic;
class GFG {
public class Node {
public int data;
public Node left, right;
public Node( int x)
{
data = x;
left = right = null ;
}
}
static Node buildTree( int [] init, int [] post, int n)
{
Stack<Node> st = new Stack<Node>();
HashSet<Node> s = new HashSet<Node>();
Node root = null ;
for ( int p = n - 1, i = n - 1; p >= 0;) {
Node node = null ;
do {
node = new Node(post[p]);
if (root == null ) {
root = node;
}
if (st.Count > 0) {
if (s.Contains(st.Peek())) {
s.Remove(st.Peek());
st.Peek().left = node;
st.Pop();
}
else {
st.Peek().right = node;
}
}
st.Push(node);
} while (post[p--] != init[i] && p >= 0);
node = null ;
while (st.Count > 0 && i >= 0
&& st.Peek().data == init[i]) {
node = st.Peek();
st.Pop();
i--;
}
if (node != null ) {
s.Add(node);
st.Push(node);
}
}
return root;
}
static void preOrder(Node node)
{
if (node == null )
return ;
Console.Write(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
public static void Main(String[] args)
{
int [] init = { 4, 8, 2, 5, 1, 6, 3, 7 };
int [] post = { 8, 4, 5, 2, 6, 7, 3, 1 };
int n = init.Length;
Node root = buildTree(init, post, n);
Console.Write(
"Preorder of the constructed tree : \n" );
preOrder(root);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function buildTree(inorder, postorder, n) {
let st = [];
let s = new Set();
let postIndex = n - 1;
let root = null ;
for (let p = n - 1, i = n - 1; p >= 0; ) {
let node = null ;
do {
node = new Node(postorder[p]);
if (root === null ) {
root = node;
}
if (st.length > 0)
{
if (s.has(st[st.length - 1])) {
s. delete (st[st.length - 1]);
st[st.length - 1].left = node;
st.pop();
} else {
st[st.length - 1].right = node;
}
}
st.push(node);
} while (postorder[p--] !== inorder[i] && p >= 0);
node = null ;
while (
st.length > 0 &&
i >= 0 &&
st[st.length - 1].data === inorder[i]
) {
node = st[st.length - 1];
st.pop();
i--;
}
if (node !== null ) {
s.add(node);
st.push(node);
}
}
return root;
}
function preOrder(node) {
if (node === null ) return ;
console.log(node.data + " " );
preOrder(node.left);
preOrder(node.right);
}
const inorder = [4, 8, 2, 5, 1, 6, 3, 7];
const postorder = [8, 4, 5, 2, 6, 7, 3, 1];
const n = inorder.length;
const root = buildTree(inorder, postorder, n);
console.log( "Preorder of the constructed tree: " );
preOrder(root);
|
Output
Preorder of the constructed tree :
1 2 4 8 5 3 6 7
Time Complexity: O(N)
Auxiliary Space: O(N), The extra space is used to store the elements in the stack and set.
Share your thoughts in the comments
Please Login to comment...