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
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:
- 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.
- 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.
- 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.
- 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 <bits/stdc++.h> 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[0] == inorder[0]) && (inorder[0] == postorder[0]); // search for first element of preorder // in inorder array int idx = -1; for ( int i = 0; i < len; ++i) if (inorder[i] == preorder[0]) { 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[0]); int len2 = sizeof (preorder) / sizeof (preorder[0]); int len3 = sizeof (postorder) / sizeof (postorder[0]); // 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 < s1 + len; ++i) if (inorder[i] == preorder[s]) { idx = i; break ; } if (idx == - 1 ) return false ; idx = idx - s1; // 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
<?php // PHP program to check if the given // three traversals are of the same // tree or not // Function to check if traversals are // of the same tree function checktree( $preorder , $inorder , $postorder , $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 [0] == $inorder [0]) && ( $inorder [0] == $postorder [0]); // search for first element of preorder // in inorder array $idx = -1; for ( $i = 0; $i < $len ; ++ $i ) if ( $inorder [ $i ] == $preorder [0]) { $idx = $i ; break ; } if ( $idx == -1) return 0; // check for the left subtree $ret1 = checktree( array_slice ( $preorder ,1), $inorder , $postorder , $idx ); // check for the right subtree $ret2 = checktree( array_slice ( $preorder , $idx + 1), array_slice ( $inorder , $idx + 1), array_slice ( $postorder , $idx ), $len - $idx - 1); // return 1 only if both of them are // correct else 0 return ( $ret1 && $ret2 ); } // Driver Code // Traversal Arrays $inorder = array ( 4, 2, 5, 1, 3 ); $preorder = array ( 1, 2, 4, 5, 3 ); $postorder = array ( 4, 5, 2, 3, 1 ); $len1 = count ( $inorder ) ; $len2 = count ( $preorder ) ; $len3 = count ( $postorder ) ; // Check if all the array lengths are equal if (( $len1 == $len2 ) && ( $len2 == $len3 )) { $res = checktree( $preorder , $inorder , $postorder , $len1 ); if ( $res ) echo "Yes" ; else echo "No" ; } else echo "No\n" ; // This code is contributed by AnkitRai01 ?> |
Output:
Yes
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.