In this c programming tutorial we are going to see how we can compare two trees to check whether they are identical or not. Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.

Below is the simple Algorithm to compare two trees:

Time Complexity :

Complexity of the identicalTree() will be according to the tree with lesser number of nodes. Let number of nodes in two trees be m and n then complexity of sameTree() is O(m) where m < n.

Program Code :

Output :

comparing 2 trees

Related Post

Leave a Reply