Introduction
In this blog, we will look at the approach to check whether the given preorder, inorder and postorder traversals are of the same tree or not. If a given order traversals are the same, our function will return a true value and if not then, the function will return a false value.

Problem Statement
Given: Preorder, inorder and postorder traversals of tree T.
Problem: Check whether the given preorder, inorder and postorder traversals are of the same tree or not. If they are the same, the function will return a true value and if not then, the function will return a false value.
Sample Examples
Let us take a look at the tree given below and check whether the given preorder, inorder and postorder traversals are of the same tree or not.

In this example, the order traversals for the tree are given as follows:
Preorder - 25 20 10 15 30
Inorder - 10 20 15 25 30
Postorder - 10 15 20 30 25
When we check whether the given preorder, inorder and postorder traversals are of the same tree or not, we will get a true value or a yes as an output because all of the given traversals are of the same tree.
Brute Force Approach
In the brute force approach, we can check whether the given preorder, inorder and postorder traversals are of the same tree or not by constructing a tree using two out of the given order traversals and then traverse the constructed tree while comparing to the third given order traversal. If both the traversals are similar, then we know that all the traversal orders are of the same tree.
Pseudocode
The pseudocode is as follows:
- Take the input of the order traversals and store each traversal order in an array, say inOrder[], preOrder[], and postOrder[].
- Now, to construct a tree out of the two given orders, pick an initial element from preOrder[] and increment a preorder index variable to pick the following element in the next recursive call of the function.
- Create a new tree node with the data of the element picked up in the previous step.
- Find the chosen element in the inOrder[] array and store the inorder index in a variable.
- Call the function to build the tree for elements before the inorder index and make it as the left subtree of the tree node.
- Call the function to build the tree for elements after the inorder index and make it as the right subtree of the tree node.
- Return the tree node.
Implementation
C++ Code:
// Program to check whether the given preorder, inorder and postorder traversals are of the same tree or not
#include <bits/stdc++.h>
using namespace std;
// structure for tree node
struct Tnode
{
int info;
struct Tnode *left, *right;
};
// Function to create a new tree node
Tnode* newTnode(int info)
{
Tnode *temp = new Tnode;
temp->info = info;
temp->left = temp->right = NULL;
return temp;
}
/* Function to find index of value in arr[] */
int search(int arr[], int strt, int end, int value)
{
for (int i = strt; i <= end; i++)
{
if(arr[i] == value)
return i;
}
}
/* Function to construct binary tree from preorder and inorder traversals.*/
Tnode* buildTree(int in[], int pre[], int inStrt,
int inEnd)
{
static int preIndex = 0;
if(inStrt > inEnd)
return NULL;
Tnode *tNode = newTnode(pre[preIndex++]);
if (inStrt == inEnd)
return tNode;
int inIndex = search(in, inStrt, inEnd, tNode->info);
/* Using index in Inorder traversal,
construct left and right subtrees */
tNode->left = buildTree(in, pre, inStrt, inIndex-1);
tNode->right = buildTree(in, pre, inIndex+1, inEnd);
return tNode;
}
/* function to compare Postorder traversal
on constructed tree and given Postorder */
int checkPostorder(Tnode* node, int postOrder[], int index)
{
if (node == NULL)
return index;
index = checkPostorder(node->left,postOrder,index);
index = checkPostorder(node->right,postOrder,index);
if (node->info == postOrder[index])
index++;
else
return -1;
return index;
}
//Main program
int main()
{
int inOrder[] = {10, 20, 15, 25, 30};
int preOrder[] = {25, 20, 10, 15, 30};
int postOrder[] = {10, 15, 20, 30, 25};
int len = sizeof(inOrder)/sizeof(inOrder[0]);
// build tree from given inorder and preorder traversals
Tnode *root = buildTree(inOrder, preOrder, 0, len - 1);
// compare postorder traversal on constructed
// tree with given Postorder traversal
int index = checkPostorder(root,postOrder,0);
// If both postorder traversals are same
if (index == len)
cout << "Yes, the traversals are of the same tree";
else
cout << "No, the traversals are not of the same tree";
return 0;
}Python Code:
# Program to check whether the given preorder, inorder and postorder traversals are of the same tree or not
class node:
def __init__(self, x):
self.info = x
self.left = None
self.right = None
preIndex = 0
def search(arr, strt, end, value):
for i in range(strt, end + 1):
if(arr[i] == value):
return i
# Function to construct binary tree from preorder and inorder traversals
def buildTree(inn, pre, inStrt, inEnd):
global preIndex
if(inStrt > inEnd):
return None
tNode = node(pre[preIndex])
preIndex += 1
if (inStrt == inEnd):
return tNode
inIndex = search(inn, inStrt,
inEnd, tNode.info)
tNode.left = buildTree(inn, pre, inStrt,
inIndex - 1)
tNode.right = buildTree(inn, pre,
inIndex + 1, inEnd)
return tNode
# function to compare postorder traversal on constructed tree
def checkPostorder(node, postOrder, index):
if (node == None):
return index
index = checkPostorder(node.left,
postOrder,
index)
index = checkPostorder(node.right,
postOrder,
index)
if (node.info == postOrder[index]):
index += 1
else:
return - 1
return index
# Main program
if __name__ == '__main__':
inOrder = [10, 20, 15, 25, 30]
preOrder = [25, 20, 10, 15, 30]
postOrder = [10, 15, 20, 30, 25]
lenn = len(inOrder)
# build tree from given
# Inorder and Preorder traversals
root = buildTree(inOrder, preOrder,
0, lenn - 1)
# compare postorder traversal on
# constructed tree with given
# Postorder traversal
index = checkPostorder(root, postOrder, 0)
# If both postorder traversals are same
if (index == lenn):
print("Yes, the traversals are of the same tree")
else:
print("No, the traversals are not of the same tree")
Output: Yes, the traversals are of the same tree
Complexity Analysis
Time Complexity: In the brute force approach, we have used two iterative loops traversing over n number of nodes, therefore, the time complexity for the code is O(n2).
Space Complexity: The auxiliary space used for n nodes is O(n), hence the space complexity is O(n).




