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

Given Preorder, Inorder and Postorder traversals of some tree. The task is 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.

We already have discussed an approach to solve the above problem by constructing a tree using any two traversals in the previous article.

In this article, an approach without using any extra space is discussed.

Approach:

1. Search for the first element of preorder array in the inorder array and store it’s index as idx, if it doesn’t exist then return False.
2. Everything from 0th index for inorder and postorder and from 1st index for preorder of length idx becomes left subtree for first element of the preorder array.
3. Everything from position idx+1 for inorder and preorder and from idx for postorder of length (length-idx-1) becomes right subtree for first element of preorder array.
4. Repeat the steps 1 to 3 recursively until length of arrays become either 0 (in which case we
return true) or 1 (in which case we return True only if all three arrays are equal, else False).

Below is the implementation of the above approach:

## C++

 `// C++ program to check if the given ` `// three traversals are of the same ` `// tree or not ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if traversals are ` `// of the same tree ` `int` `checktree(``int` `preorder[], ``int` `inorder[], ` `              ``int` `postorder[], ``int` `len) ` `{ ` ` `  `    ``// if the array lengths are 0, ` `    ``// then all of them are obviously equal ` `    ``if` `(len == 0) ` `        ``return` `1; ` ` `  `    ``// if array lengths are 1, ` `    ``// then check if all of them are equal ` `    ``if` `(len == 1) ` `        ``return` `(preorder == inorder) ` `               ``&& (inorder == postorder); ` ` `  `    ``// search for first element of preorder ` `    ``// in inorder array ` `    ``int` `idx = -1; ` `    ``for` `(``int` `i = 0; i < len; ++i) ` `        ``if` `(inorder[i] == preorder) { ` `            ``idx = i; ` `            ``break``; ` `        ``} ` ` `  `    ``if` `(idx == -1) ` `        ``return` `0; ` ` `  `    ``// check for the left subtree ` `    ``int` `ret1 = checktree(preorder + 1, inorder, ` `                         ``postorder, idx); ` ` `  `    ``// check for the right subtree ` `    ``int` `ret2 = checktree(preorder + idx + 1, inorder + idx + 1, ` `                         ``postorder + idx, len - idx - 1); ` ` `  `    ``// return 1 only if both of them are ` `    ``// correct else 0 ` `    ``return` `(ret1 && ret2); ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Traversal Arrays ` `    ``int` `inorder[] = { 4, 2, 5, 1, 3 }; ` `    ``int` `preorder[] = { 1, 2, 4, 5, 3 }; ` `    ``int` `postorder[] = { 4, 5, 2, 3, 1 }; ` `    ``int` `len1 = ``sizeof``(inorder) / ``sizeof``(inorder); ` `    ``int` `len2 = ``sizeof``(preorder) / ``sizeof``(preorder); ` `    ``int` `len3 = ``sizeof``(postorder) / ``sizeof``(postorder); ` ` `  `    ``// Check if all the array lengths are equal ` `    ``if` `((len1 == len2) && (len2 == len3)) { ` ` `  `        ``bool` `res = checktree(preorder, inorder, ` `                             ``postorder, len1); ` ` `  `        ``(res) ? cout << ``"Yes"` `: cout << ``"No"``; ` `    ``} ` `    ``else` `        ``cout << ``"No\n"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to check if the given ` `// three traversals are of the same ` `// tree or not ` `class` `GFG ` `{ ` ` `  `// Function to check if traversals are ` `// of the same tree ` `static` `boolean` `checktree(``int` `preorder[],``int` `s, ` `                        ``int` `inorder[],``int` `s1, ` `                        ``int` `postorder[],``int` `s2, ``int` `len) ` `{ ` ` `  `    ``// if the array lengths are 0, ` `    ``// then all of them are obviously equal ` `    ``if` `(len == ``0``) ` `        ``return` `true``; ` ` `  `    ``// if array lengths are 1, ` `    ``// then check if all of them are equal ` `    ``if` `(len == ``1``) ` `        ``return` `((preorder[s] == inorder[s1]) ` `            ``&& (inorder[s1] == postorder[s2])); ` ` `  `    ``// search for first element of preorder ` `    ``// in inorder array ` `    ``int` `idx = -``1``; ` `    ``for` `(``int` `i = s1; i < len; ++i) ` `        ``if` `(inorder[i] == preorder[s])  ` `        ``{ ` `            ``idx = i; ` `            ``break``; ` `        ``} ` ` `  `    ``if` `(idx == -``1``) ` `        ``return` `false``; ` ` `  `    ``// check for the left subtree ` `    ``boolean` `ret1 = checktree(preorder ,s + ``1``,  ` `                    ``inorder,s1,postorder,s2, idx); ` ` `  `    ``// check for the right subtree ` `    ``boolean` `ret2 = checktree(preorder ,s + idx + ``1``,  ` `                                ``inorder ,s1+ idx + ``1``, ` `                                ``postorder,s2 + idx, len - idx - ``1``); ` ` `  `    ``// return 1 only if both of them are ` `    ``// correct else 0 ` `    ``return` `(ret1 && ret2); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``// Traversal Arrays ` `    ``int` `inorder[] = { ``4``, ``2``, ``5``, ``1``, ``3` `}; ` `    ``int` `preorder[] = { ``1``, ``2``, ``4``, ``5``, ``3` `}; ` `    ``int` `postorder[] = { ``4``, ``5``, ``2``, ``3``, ``1` `}; ` `    ``int` `len1 = inorder.length; ` `    ``int` `len2 = preorder.length; ` `    ``int` `len3 = postorder.length; ` ` `  `    ``// Check if all the array lengths are equal ` `    ``if` `((len1 == len2) && (len2 == len3))  ` `    ``{ ` ` `  `        ``boolean` `res = checktree(preorder, ``0``, inorder, ``0``, ` `                            ``postorder, ``0``, len1); ` ` `  `        ``System.out.print(( (res) ? ``"Yes"` `: ``"No"``)); ` `    ``} ` `    ``else` `        ``System.out.print( ``"No\n"``); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python

 `# Python program to check if the given  ` `# three traversals are of the same  ` `# tree or not ` ` `  `# Function to check if all three traversals ` `# are of the same tree ` `def` `checktree(preorder, inorder, postorder, length): ` `     `  `    ``# if the array lengths are 0,  ` `    ``# then all of them are obviously equal ` `    ``if` `length ``=``=` `0``:  ` `        ``return` `1` `         `  `    ``# if array lengths are 1,  ` `    ``# then check if all of them are equal ` `    ``if` `length ``=``=` `1``:  ` `        ``return` `(preorder[``0``] ``=``=` `inorder[``0``]) ``and` `(inorder[``0``] ``=``=` `postorder[``0``]); ` ` `  `    ``# search for first element of preorder  ` `    ``# in inorder array ` `    ``idx ``=` `-``1``; ` `     `  `    ``for` `i ``in` `range``(length): ` `        ``if` `inorder[i] ``=``=` `preorder[``0``]: ` `            ``idx ``=` `i ` `            ``break` `     `  `    ``if` `idx ``=``=` `-``1``: ` `        ``return` `0``; ` `     `  `    ``# check for the left subtree ` `    ``ret1 ``=` `checktree(preorder[``1``:], inorder, postorder, idx); ` `     `  `    ``# check for the right subtree      ` `    ``ret2 ``=` `checktree(preorder[idx ``+` `1``:], inorder[idx ``+` `1``:],  ` `                           ``postorder[idx:], length``-``idx``-``1``); ` `     `  `    ``# return 1 only if both of them are correct else 0 ` `    ``return` `(ret1 ``and` `ret2) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``inorder ``=` `[``4``, ``2``, ``5``, ``1``, ``3``]  ` `    ``preorder ``=` `[``1``, ``2``, ``4``, ``5``, ``3``]  ` `    ``postorder ``=` `[``4``, ``5``, ``2``, ``3``, ``1``] ` `    ``len1 ``=` `len``(inorder) ` `    ``len2 ``=` `len``(preorder) ` `    ``len3 ``=` `len``(postorder) ` ` `  `    ``# check if all the array lengths are equal ` `    ``if` `(len1 ``=``=` `len2) ``and` `(len2 ``=``=` `len3): ` `        ``correct ``=` `checktree(preorder, inorder,  ` `                                ``postorder, len1) ` `        ``if` `(correct):  ` `            ``print``(``"Yes"``)  ` `        ``else``:  ` `            ``print``(``"No"``) ` `    ``else``: ` `        ``print``(``"No"``); `

## C#

 `// C# program to check if the given ` `// three traversals are of the same ` `// tree or not ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to check if traversals are ` `// of the same tree ` `static` `bool` `checktree(``int` `[]preorder,``int` `s, ` `                        ``int` `[]inorder,``int` `s1, ` `                        ``int` `[]postorder,``int` `s2, ``int` `len) ` `{ ` ` `  `    ``// if the array lengths are 0, ` `    ``// then all of them are obviously equal ` `    ``if` `(len == 0) ` `        ``return` `true``; ` ` `  `    ``// if array lengths are 1, ` `    ``// then check if all of them are equal ` `    ``if` `(len == 1) ` `        ``return` `((preorder[s] == inorder[s1]) ` `            ``&& (inorder[s1] == postorder[s2])); ` ` `  `    ``// search for first element of preorder ` `    ``// in inorder array ` `    ``int` `idx = -1; ` `    ``for` `(``int` `i = s1; i < len; ++i) ` `        ``if` `(inorder[i] == preorder[s])  ` `        ``{ ` `            ``idx = i; ` `            ``break``; ` `        ``} ` ` `  `    ``if` `(idx == -1) ` `        ``return` `false``; ` ` `  `    ``// check for the left subtree ` `    ``bool` `ret1 = checktree(preorder ,s + 1,  ` `                    ``inorder,s1,postorder,s2, idx); ` ` `  `    ``// check for the right subtree ` `    ``bool` `ret2 = checktree(preorder ,s + idx + 1,  ` `                                ``inorder ,s1+ idx + 1, ` `                                ``postorder,s2 + idx, len - idx - 1); ` ` `  `    ``// return 1 only if both of them are ` `    ``// correct else 0 ` `    ``return` `(ret1 && ret2); ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``// Traversal Arrays ` `    ``int` `[]inorder = { 4, 2, 5, 1, 3 }; ` `    ``int` `[]preorder = { 1, 2, 4, 5, 3 }; ` `    ``int` `[]postorder = { 4, 5, 2, 3, 1 }; ` `    ``int` `len1 = inorder.Length; ` `    ``int` `len2 = preorder.Length; ` `    ``int` `len3 = postorder.Length; ` `     `  `    ``// Check if all the array lengths are equal ` `    ``if` `((len1 == len2) && (len2 == len3))  ` `    ``{ ` `     `  `        ``bool` `res = checktree(preorder, 0, inorder, 0, ` `                            ``postorder, 0, len1); ` `     `  `        ``Console.Write(( (res) ? ``"Yes"` `: ``"No"``)); ` `    ``} ` `    ``else` `        ``Console.Write( ``"No\n"``); ` `} ` `} ` ` `  ` ``// This code is contributed by ajit `

## PHP

 ` `

Output:

```Yes
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

4

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