Construct Full Binary Tree from given preorder and postorder traversals
Given two arrays that represent preorder and postorder traversals of a full binary tree, construct the binary tree. Full Binary Tree is a binary tree where every node has either 0 or 2 children.
Illustration: Following are examples of Full Trees.
1
/ \
2 3
/ \ / \
4 5 6 7
1
/ \
2 3
/ \
4 5
/ \
6 7
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
It is not possible to construct a general Binary Tree from preorder and postorder traversals (See this). But if know that the Binary Tree is Full, we can construct the tree without ambiguity. Let us understand this with the help of the following example.
Let us consider the two given arrays as pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7} and post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
In pre[], the leftmost element is root of tree. Since the tree is full and array size is more than 1. The value next to 1 in pre[], must be left child of root. So we know 1 is root and 2 is left child. How to find the all nodes in left subtree? We know 2 is root of all nodes in left subtree. All nodes before 2 in post[] must be in left subtree. Now we know 1 is root, elements {8, 9, 4, 5, 2} are in left subtree, and the elements {6, 7, 3} are in right subtree.
1
/ \
/ \
{8, 9, 4, 5, 2} {6, 7, 3}
We recursively follow the above approach and get the following tree.
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
int data;
node* left;
node* right;
};
node* newNode( int data)
{
node* temp = new node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
node* constructTreeUtil( int pre[], int post[],
int * preIndex, int l, int h,
int size)
{
if (*preIndex >= size || l > h)
return NULL;
node* root = newNode(pre[*preIndex]);
++*preIndex;
if (l == h)
return root;
int i;
for (i = l; i <= h; ++i)
if (pre[*preIndex] == post[i])
break ;
if (i <= h) {
root->left = constructTreeUtil(pre, post, preIndex,
l, i, size);
root->right = constructTreeUtil(pre, post, preIndex,
i + 1, h - 1, size);
}
return root;
}
node* constructTree( int pre[], int post[], int size)
{
int preIndex = 0;
return constructTreeUtil(pre, post, &preIndex, 0,
size - 1, size);
}
void printInorder(node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
cout << node->data << " " ;
printInorder(node->right);
}
int main()
{
int pre[] = { 1, 2, 4, 8, 9, 5, 3, 6, 7 };
int post[] = { 8, 9, 4, 5, 2, 6, 7, 3, 1 };
int size = sizeof (pre) / sizeof (pre[0]);
node* root = constructTree(pre, post, size);
cout << "Inorder traversal of the constructed tree: \n" ;
printInorder(root);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node* newNode ( int data)
{
struct node* temp = ( struct node *) malloc ( sizeof ( struct node) );
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
struct node* constructTreeUtil ( int pre[], int post[], int * preIndex,
int l, int h, int size)
{
if (*preIndex >= size || l > h)
return NULL;
struct node* root = newNode ( pre[*preIndex] );
++*preIndex;
if (l == h)
return root;
int i;
for (i = l; i <= h; ++i)
if (pre[*preIndex] == post[i])
break ;
if (i <= h)
{
root->left = constructTreeUtil (pre, post, preIndex,
l, i, size);
root->right = constructTreeUtil (pre, post, preIndex,
i + 1, h-1, size);
}
return root;
}
struct node *constructTree ( int pre[], int post[], int size)
{
int preIndex = 0;
return constructTreeUtil (pre, post, &preIndex, 0, size - 1, size);
}
void printInorder ( struct node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
printf ( "%d " , node->data);
printInorder(node->right);
}
int main ()
{
int pre[] = {1, 2, 4, 8, 9, 5, 3, 6, 7};
int post[] = {8, 9, 4, 5, 2, 6, 7, 3, 1};
int size = sizeof ( pre ) / sizeof ( pre[0] );
struct node *root = constructTree(pre, post, size);
printf ( "Inorder traversal of the constructed tree: \n" );
printInorder(root);
return 0;
}
|
Java
public class fullbinarytreepostpre
{
static int preindex;
static class node
{
int data;
node left, right;
public node( int data)
{
this .data = data;
}
}
static node constructTreeUtil( int pre[], int post[], int l,
int h, int size)
{
if (preindex >= size || l > h)
return null ;
node root = new node(pre[preindex]);
preindex++;
if (l == h || preindex >= size)
return root;
int i;
for (i = l; i <= h; i++)
{
if (post[i] == pre[preindex])
break ;
}
if (i <= h)
{
root.left = constructTreeUtil(pre, post, l, i, size);
root.right = constructTreeUtil(pre, post, i + 1 , h- 1 , size);
}
return root;
}
static node constructTree( int pre[], int post[], int size)
{
preindex = 0 ;
return constructTreeUtil(pre, post, 0 , size - 1 , size);
}
static void printInorder(node root)
{
if (root == null )
return ;
printInorder(root.left);
System.out.print(root.data + " " );
printInorder(root.right);
}
public static void main(String[] args)
{
int pre[] = { 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 };
int post[] = { 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 };
int size = pre.length;
node root = constructTree(pre, post, size);
System.out.println( "Inorder traversal of the constructed tree:" );
printInorder(root);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def constructTreeUtil(pre: list , post: list ,
l: int , h: int ,
size: int ) - > Node:
global preIndex
if (preIndex > = size or l > h):
return None
root = Node(pre[preIndex])
preIndex + = 1
if (l = = h or preIndex > = size):
return root
i = l
while i < = h:
if (pre[preIndex] = = post[i]):
break
i + = 1
if (i < = h):
root.left = constructTreeUtil(pre, post,
l, i, size)
root.right = constructTreeUtil(pre, post,
i + 1 , h - 1 ,
size)
return root
def constructTree(pre: list ,
post: list ,
size: int ) - > Node:
global preIndex
return constructTreeUtil(pre, post, 0 ,
size - 1 , size)
def printInorder(node: Node) - > None :
if (node is None ):
return
printInorder(node.left)
print (node.data, end = " " )
printInorder(node.right)
if __name__ = = "__main__" :
pre = [ 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 ]
post = [ 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 ]
size = len (pre)
preIndex = 0
root = constructTree(pre, post, size)
print ( "Inorder traversal of "
"the constructed tree: " )
printInorder(root)
|
C#
using System;
class GFG
{
public static int preindex;
public class node
{
public int data;
public node left, right;
public node( int data)
{
this .data = data;
}
}
public static node constructTreeUtil( int [] pre, int [] post,
int l, int h, int size)
{
if (preindex >= size || l > h)
{
return null ;
}
node root = new node(pre[preindex]);
preindex++;
if (l == h || preindex >= size)
{
return root;
}
int i;
for (i = l; i <= h; i++)
{
if (post[i] == pre[preindex])
{
break ;
}
}
if (i <= h)
{
root.left = constructTreeUtil(pre, post,
l, i, size);
root.right = constructTreeUtil(pre, post,
i + 1, h-1, size);
}
return root;
}
public static node constructTree( int [] pre,
int [] post, int size)
{
preindex = 0;
return constructTreeUtil(pre, post, 0, size - 1, size);
}
public static void printInorder(node root)
{
if (root == null )
{
return ;
}
printInorder(root.left);
Console.Write(root.data + " " );
printInorder(root.right);
}
public static void Main( string [] args)
{
int [] pre = new int [] {1, 2, 4, 8, 9, 5, 3, 6, 7};
int [] post = new int [] {8, 9, 4, 5, 2, 6, 7, 3, 1};
int size = pre.Length;
node root = constructTree(pre, post, size);
Console.WriteLine( "Inorder traversal of " +
"the constructed tree:" );
printInorder(root);
}
}
|
Javascript
<script>
var preindex = 0;
class node
{
constructor(data)
{
this .data = data;
}
}
function constructTreeUtil(pre, post, l, h, size)
{
if (preindex >= size || l > h)
{
return null ;
}
var root = new node(pre[preindex]);
preindex++;
if (l == h || preindex >= size)
{
return root;
}
var i;
for (i = l; i <= h; i++)
{
if (post[i] == pre[preindex])
{
break ;
}
}
if (i <= h)
{
root.left = constructTreeUtil(pre, post,
l, i, size);
root.right = constructTreeUtil(pre, post,
i + 1, h-1, size);
}
return root;
}
function constructTree(pre, post, size)
{
preindex = 0;
return constructTreeUtil(pre, post, 0, size - 1, size);
}
function printInorder(root)
{
if (root == null )
{
return ;
}
printInorder(root.left);
document.write(root.data + " " );
printInorder(root.right);
}
var pre = [1, 2, 4, 8, 9, 5, 3, 6, 7];
var post = [8, 9, 4, 5, 2, 6, 7, 3, 1];
var size = pre.length;
var root = constructTree(pre, post, size);
document.write( "Inorder traversal of " +
"the constructed tree:<br>" );
printInorder(root);
</script>
|
Output
Inorder traversal of the constructed tree:
8 4 9 2 5 1 6 3 7
Time Complexity: O(h), Space Auxiliary Space :O(h)
// h is the height of the tree.
Last Updated :
21 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...