Create a binary tree from post order traversal and leaf node array
Last Updated :
26 Nov, 2021
Given 2 arrays, the first one containing postorder traversal sequence and the second one containing the information whether the corresponding node in the first array is a leaf node or a non-leaf node, create a binary tree and return its root and print it’s inorder traversal. (There can be more than one tree possible, but you have to form only one tree)
Examples:
Input:
postorder = {40, 20, 50, 60, 30, 10}
isLeaf = {true, false, true, true, false, false}
Output: 20 40 10 50 30 60
Explanation:
Generated Binary tree
10
/ \
20 30
\ / \
40 50 60
Input:
postorder = {20, 18, 25, 100, 81, 15, 7}
isLeaf = {true, false, true, true, false, false, false}
Output: 7 18 20 15 25 81 100
Explanation:
Generated Binary tree
7
\
15
/ \
18 81
\ / \
20 25 100
Approach:
The idea is to first construct the root node of the binary tree using the last key in the post-order sequence. Then using the given boolean array, we find if the root node is an internal node or a leaf node. If the root node is an internal node, we recursively construct its right and left subtrees.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
struct Tree {
int val;
Tree* leftchild;
Tree* rightchild;
Tree( int _val, Tree* _leftchild, Tree* _rightchild)
{
val = _val;
leftchild = _leftchild;
rightchild = _rightchild;
}
};
struct Tree* createBinaryTree( int post[], bool isLeaf[], int & n)
{
if (n < 0){
return NULL;
}
struct Tree* root = new Tree(post[n], NULL, NULL);
bool isInternalNode = !isLeaf[n];
n--;
if (isInternalNode) {
root->rightchild = createBinaryTree(post, isLeaf, n);
root->leftchild = createBinaryTree(post, isLeaf, n);
}
return root;
}
void inorder( struct Tree* root)
{
if (root == NULL){
return ;
}
inorder(root->leftchild);
cout << root->val << " " ;
inorder(root->rightchild);
}
int main()
{
int post[] = { 40, 20, 50, 60, 30, 10 };
bool isLeaf[] = { true , false , true , true , false , false };
int n = sizeof (post) / sizeof (post[0]) - 1;
struct Tree* root = createBinaryTree(post, isLeaf, n);
inorder(root);
return 0;
}
|
Java
class GFG
{
static int n;
static class Tree
{
int val;
Tree leftchild;
Tree rightchild;
Tree( int _val, Tree _leftchild,
Tree _rightchild)
{
val = _val;
leftchild = _leftchild;
rightchild = _rightchild;
}
};
static Tree createBinaryTree( int post[],
boolean isLeaf[])
{
if (n < 0 )
{
return null ;
}
Tree root = new Tree(post[n], null , null );
boolean isInternalNode = !isLeaf[n];
n--;
if (isInternalNode)
{
root.rightchild = createBinaryTree(post, isLeaf);
root.leftchild = createBinaryTree(post, isLeaf);
}
return root;
}
static void inorder(Tree root)
{
if (root == null )
{
return ;
}
inorder(root.leftchild);
System.out.print(root.val + " " );
inorder(root.rightchild);
}
public static void main(String[] args)
{
int post[] = { 40 , 20 , 50 , 60 , 30 , 10 };
boolean isLeaf[] = { true , false , true ,
true , false , false };
n = post.length - 1 ;
Tree root = createBinaryTree(post, isLeaf);
inorder(root);
}
}
|
Python3
class Tree:
def __init__( self , key):
self .val = key
self .leftchild = self .rightchild = None
n = 0
def createBinaryTree( post, isLeaf):
global n
if (n < 0 ):
return None
root = Tree(post[n])
isInternalNode = not isLeaf[n]
n = n - 1
if (isInternalNode):
root.rightchild = createBinaryTree(post, isLeaf)
root.leftchild = createBinaryTree(post, isLeaf)
return root
def inorder( root):
if (root = = None ):
return
inorder(root.leftchild)
print ( root.val ,end = " " )
inorder(root.rightchild)
post = [ 40 , 20 , 50 , 60 , 30 , 10 ]
isLeaf = [ True , False , True , True , False , False ]
n = len (post) - 1
root = createBinaryTree(post, isLeaf)
inorder(root)
|
C#
using System;
class GFG
{
static int n;
public class Tree
{
public int val;
public Tree leftchild;
public Tree rightchild;
public Tree( int _val, Tree _leftchild,
Tree _rightchild)
{
val = _val;
leftchild = _leftchild;
rightchild = _rightchild;
}
};
static Tree createBinaryTree( int []post,
Boolean []isLeaf)
{
if (n < 0)
{
return null ;
}
Tree root = new Tree(post[n], null , null );
Boolean isInternalNode = !isLeaf[n];
n--;
if (isInternalNode)
{
root.rightchild = createBinaryTree(post,
isLeaf);
root.leftchild = createBinaryTree(post,
isLeaf);
}
return root;
}
static void inorder(Tree root)
{
if (root == null )
{
return ;
}
inorder(root.leftchild);
Console.Write(root.val + " " );
inorder(root.rightchild);
}
public static void Main(String[] args)
{
int []post = { 40, 20, 50, 60, 30, 10 };
Boolean []isLeaf = { true , false , true ,
true , false , false };
n = post.Length - 1;
Tree root = createBinaryTree(post, isLeaf);
inorder(root);
}
}
|
Javascript
<script>
var n;
class Tree
{
constructor(_val, _leftchild, _rightchild)
{
this .val = _val;
this .leftchild = _leftchild;
this .rightchild = _rightchild;
}
};
function createBinaryTree(post, isLeaf)
{
if (n < 0)
{
return null ;
}
var root = new Tree(post[n], null , null );
var isInternalNode = !isLeaf[n];
n--;
if (isInternalNode)
{
root.rightchild = createBinaryTree(post,
isLeaf);
root.leftchild = createBinaryTree(post,
isLeaf);
}
return root;
}
function inorder(root)
{
if (root == null )
{
return ;
}
inorder(root.leftchild);
document.write(root.val + " " );
inorder(root.rightchild);
}
var post = [40, 20, 50, 60, 30, 10];
var isLeaf = [ true , false , true ,
true , false , false ];
n = post.length - 1;
var root = createBinaryTree(post, isLeaf);
inorder(root);
</script>
|
Output:
20 40 10 50 30 60
Time Complexity: O(N).
Auxiliary Space: O(N).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...