# Check if given Preorder, Inorder and Postorder traversals are of same tree

Given Preorder , Inorder and Postorder traversals of some tree. Write a program to check if they all are of the same tree.

Examples:

```Input : Inorder -> 4 2 5 1 3
Preorder -> 1 2 4 5 3
Postorder -> 4 5 2 3 1
Output : Yes
Exaplanation : All of the above three traversals are of
the same tree              1
/   \
2     3
/   \
4     5

Input : Inorder -> 4 2 5 1 3
Preorder -> 1 5 4 2 3
Postorder -> 4 1 2 3 5
Output : No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The most basic approach to solve this problem will be to first construct a tree using two of the three given traversals and then do the third traversal on this constructed tree and compare it with the given traversal. If both of the traversals are same then print Yes otherwise print No. Here, we use Inorder and Preorder traversals to construct the tree. We may also use Inorder and Postorder traversal instead of Preorder traversal for tree construction. You may refer to this post on how to construct tree from given Inorder and Preorder traversal. After constructing the tree, we will obtain the Postorder traversal of this tree and compare it with the given Postorder traversal.

Below is the implementation of above approach:

## C++

 `/* C++ program to check if all three given ` `   ``traversals are of the same tree */` `#include ` `using` `namespace` `std; ` ` `  `// A Binary Tree Node ` `struct` `Node ` `{ ` `    ``int` `data; ` `    ``struct` `Node *left, *right; ` `}; ` ` `  `// Utility function to create a new tree node ` `Node* newNode(``int` `data) ` `{ ` `    ``Node *temp = ``new` `Node; ` `    ``temp->data = data; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `temp; ` `} ` ` `  `/* Function to find index of value in arr[start...end] ` `   ``The function assumes that value is present in in[] */` `int` `search(``int` `arr[], ``int` `strt, ``int` `end, ``int` `value) ` `{ ` `    ``for` `(``int` `i = strt; i <= end; i++) ` `    ``{ ` `        ``if``(arr[i] == value) ` `            ``return` `i; ` `    ``} ` `} ` ` `  `/* Recursive function to construct binary tree  ` `   ``of size len from Inorder traversal in[] and  ` `   ``Preorder traversal pre[].  Initial values ` `   ``of inStrt and inEnd should be 0 and len -1.   ` `   ``The function doesn't do any error checking for  ` `   ``cases where inorder and preorder do not form a  ` `   ``tree */` `Node* buildTree(``int` `in[], ``int` `pre[], ``int` `inStrt,  ` `                                      ``int` `inEnd) ` `{ ` `    ``static` `int` `preIndex = 0; ` `  `  `    ``if``(inStrt > inEnd) ` `        ``return` `NULL; ` `  `  `    ``/* Pick current node from Preorder traversal  ` `       ``using preIndex and increment preIndex */` `    ``Node *tNode = newNode(pre[preIndex++]); ` `  `  `    ``/* If this node has no children then return */` `    ``if` `(inStrt == inEnd) ` `        ``return` `tNode; ` `  `  `    ``/* Else find the index of this node in  ` `       ``Inorder traversal */` `    ``int` `inIndex = search(in, inStrt, inEnd, tNode->data); ` `  `  `    ``/* Using index in Inorder traversal,  ` `       ``construct left and right subtress */` `    ``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(Node* node, ``int` `postOrder[], ``int` `index) ` `{ ` `    ``if` `(node == NULL) ` `        ``return` `index; ` `  `  `    ``/* first recur on left child */` `    ``index = checkPostorder(node->left,postOrder,index); ` `      `  `    ``/* now recur on right child */` `    ``index = checkPostorder(node->right,postOrder,index);     ` `   `  `    ``/* Compare if data at current index in  ` `       ``both Postorder traversals are same */` `    ``if` `(node->data == postOrder[index]) ` `        ``index++; ` `    ``else` `        ``return` `-1; ` ` `  `    ``return` `index; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `inOrder[] = {4, 2, 5, 1, 3}; ` `    ``int` `preOrder[] = {1, 2, 4, 5, 3}; ` `    ``int` `postOrder[] = {4, 5, 2, 3, 1}; ` ` `  `    ``int` `len = ``sizeof``(inOrder)/``sizeof``(inOrder); ` ` `  `    ``// build tree from given  ` `    ``// Inorder and Preorder traversals ` `    ``Node *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"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `/* Java program to check if all three given  ` `traversals are of the same tree */` `import` `java.util.*; ` `class` `GfG { ` `    ``static` `int` `preIndex = ``0``; ` ` `  `// A Binary Tree Node  ` `static` `class` `Node  ` `{  ` `    ``int` `data;  ` `    ``Node left, right;  ` `} ` ` `  `// Utility function to create a new tree node  ` `static` `Node newNode(``int` `data)  ` `{  ` `    ``Node temp = ``new` `Node();  ` `    ``temp.data = data;  ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``;  ` `    ``return` `temp;  ` `}  ` ` `  `/* Function to find index of value in arr[start...end]  ` `The function assumes that value is present in in[] */` `static` `int` `search(``int` `arr[], ``int` `strt, ``int` `end, ``int` `value)  ` `{  ` `    ``for` `(``int` `i = strt; i <= end; i++)  ` `    ``{  ` `        ``if``(arr[i] == value)  ` `            ``return` `i;  ` `    ``}  ` `    ``return` `-``1``; ` `}  ` ` `  `/* Recursive function to construct binary tree  ` `of size len from Inorder traversal in[] and  ` `Preorder traversal pre[]. Initial values  ` `of inStrt and inEnd should be 0 and len -1.  ` `The function doesn't do any error checking for  ` `cases where inorder and preorder do not form a  ` `tree */` `static` `Node buildTree(``int` `in[], ``int` `pre[], ``int` `inStrt, ``int` `inEnd)  ` `{  ` ` `  `    ``if``(inStrt > inEnd)  ` `        ``return` `null``;  ` ` `  `    ``/* Pick current node from Preorder traversal  ` `    ``using preIndex and increment preIndex */` `    ``Node tNode = newNode(pre[preIndex++]);  ` ` `  `    ``/* If this node has no children then return */` `    ``if` `(inStrt == inEnd)  ` `        ``return` `tNode;  ` ` `  `    ``/* Else find the index of this node in  ` `    ``Inorder traversal */` `    ``int` `inIndex = search(in, inStrt, inEnd, tNode.data);  ` ` `  `    ``/* Using index in Inorder traversal,  ` `    ``construct left and right subtress */` `    ``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 */` `static` `int` `checkPostorder(Node node, ``int` `postOrder[], ``int` `index)  ` `{  ` `    ``if` `(node == ``null``)  ` `        ``return` `index;  ` ` `  `    ``/* first recur on left child */` `    ``index = checkPostorder(node.left,postOrder,index);  ` `     `  `    ``/* now recur on right child */` `    ``index = checkPostorder(node.right,postOrder,index);      ` `     `  `    ``/* Compare if data at current index in  ` `    ``both Postorder traversals are same */` `    ``if` `(node.data == postOrder[index])  ` `        ``index++;  ` `    ``else` `        ``return` `-``1``;  ` ` `  `    ``return` `index;  ` `}  ` ` `  `// Driver program to test above functions  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `inOrder[] = {``4``, ``2``, ``5``, ``1``, ``3``};  ` `    ``int` `preOrder[] = {``1``, ``2``, ``4``, ``5``, ``3``};  ` `    ``int` `postOrder[] = {``4``, ``5``, ``2``, ``3``, ``1``};  ` ` `  `    ``int` `len = inOrder.length;  ` ` `  `    ``// build tree from given  ` `    ``// Inorder and Preorder traversals  ` `    ``Node 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)  ` `        ``System.out.println(``"Yes"``);  ` `    ``else` `        ``System.out.println(``"No"``);  ` ` `  `} ` `}  `

## C#

 `/* C# program to check if all three given  ` `traversals are of the same tree */` `using` `System; ` `     `  `public` `class` `GfG  ` `{ ` `    ``static` `int` `preIndex = 0; ` ` `  `    ``// A Binary Tree Node  ` `    ``class` `Node  ` `    ``{  ` `        ``public` `int` `data;  ` `        ``public` `Node left, right;  ` `    ``} ` ` `  `    ``// Utility function to create a new tree node  ` `    ``static` `Node newNode(``int` `data)  ` `    ``{  ` `        ``Node temp = ``new` `Node();  ` `        ``temp.data = data;  ` `        ``temp.left = ``null``; ` `        ``temp.right = ``null``;  ` `        ``return` `temp;  ` `    ``}  ` ` `  `    ``/* Function to find index of  ` `    ``value in arr[start...end]  ` `    ``The function assumes that  ` `    ``value is present in in[] */` `    ``static` `int` `search(``int` `[]arr, ``int` `strt, ` `                        ``int` `end, ``int` `value)  ` `    ``{  ` `        ``for` `(``int` `i = strt; i <= end; i++)  ` `        ``{  ` `            ``if``(arr[i] == value)  ` `                ``return` `i;  ` `        ``}  ` `        ``return` `-1; ` `    ``}  ` ` `  `    ``/* Recursive function to construct  ` `    ``binary tree of size len from Inorder ` `    ``traversal in[] and Preorder traversal ` `    ``pre[]. Initial values of inStrt and  ` `    ``inEnd should be 0 and len -1. The  ` `    ``function doesn't do any error checking for  ` `    ``cases where inorder and preorder do not form a  ` `    ``tree */` `    ``static` `Node buildTree(``int` `[]In, ``int` `[]pre, ` `                            ``int` `inStrt, ``int` `inEnd)  ` `    ``{  ` ` `  `        ``if``(inStrt > inEnd)  ` `            ``return` `null``;  ` ` `  `        ``/* Pick current node from Preorder traversal  ` `        ``using preIndex and increment preIndex */` `        ``Node tNode = newNode(pre[preIndex++]);  ` ` `  `        ``/* If this node has no children then return */` `        ``if` `(inStrt == inEnd)  ` `            ``return` `tNode;  ` ` `  `        ``/* Else find the index of this node in  ` `        ``Inorder traversal */` `        ``int` `inIndex = search(In, inStrt, inEnd, tNode.data);  ` ` `  `        ``/* Using index in Inorder traversal,  ` `        ``construct left and right subtress */` `        ``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 */` `    ``static` `int` `checkPostorder(Node node, ``int` `[]postOrder, ``int` `index)  ` `    ``{  ` `        ``if` `(node == ``null``)  ` `            ``return` `index;  ` ` `  `        ``/* first recur on left child */` `        ``index = checkPostorder(node.left,postOrder,index);  ` ` `  `        ``/* now recur on right child */` `        ``index = checkPostorder(node.right,postOrder,index);      ` ` `  `        ``/* Compare if data at current index in  ` `        ``both Postorder traversals are same */` `        ``if` `(node.data == postOrder[index])  ` `            ``index++;  ` `        ``else` `            ``return` `-1;  ` ` `  `        ``return` `index;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]inOrder = {4, 2, 5, 1, 3};  ` `        ``int` `[]preOrder = {1, 2, 4, 5, 3};  ` `        ``int` `[]postOrder = {4, 5, 2, 3, 1};  ` ` `  `        ``int` `len = inOrder.Length;  ` ` `  `        ``// build tree from given  ` `        ``// Inorder and Preorder traversals  ` `        ``Node 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)  ` `            ``Console.WriteLine(``"Yes"``);  ` `        ``else` `            ``Console.WriteLine(``"No"``);  ` ` `  `    ``} ` `} ` ` `  `/* This code is contributed PrinciRaj1992 */`

Output:

```Yes
```

Time Complexity : O( n * n ), where n is number of nodes in the tree.

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.