Check if two trees have same structure
Given two binary trees. The task is to write a program to check if the two trees are identical in structure.

In the above figure both of the trees, Tree1 and Tree2 are identical in structure. That is, they have the same structure.
Note: This problem is different from Check if two trees are identical as here we need to compare only the structures of the two trees and not the values at their nodes.
The idea is to traverse both trees simultaneously following the same paths and keep checking if a node exists for both the trees or not.
Algorithm:
- If both trees are empty then return 1.
- Else If both trees are non-empty:
- Check left subtrees recursively i.e., call isSameStructure(tree1->left_subtree, tree2->left_subtree)
- Check right subtrees recursively i.e., call isSameStructure(tree1->right_subtree, tree2->right_subtree)
- If the value returned in above two steps are true then return 1.
- Else return 0 (one is empty and other is not).
Below is the implementation of above algorithm:
// C++ program to check if two trees have // same structure #include <iostream> using namespace std; // A binary tree node has data, pointer to left child // and a pointer to right child struct Node { int data; struct Node* left; struct Node* right; }; // Helper function that allocates a new node with the // given data and NULL left and right pointers. Node* newNode(int data) { Node* node = new Node; node->data = data; node->left = NULL; node->right = NULL; return(node); } // Function to check if two trees have same // structure int isSameStructure(Node* a, Node* b) { // 1. both empty if (a==NULL && b==NULL) return 1; // 2. both non-empty -> compare them if (a!=NULL && b!=NULL) { return ( isSameStructure(a->left, b->left) && isSameStructure(a->right, b->right) ); } // 3. one empty, one not -> false return 0; } // Driver code int main() { Node *root1 = newNode(10); Node *root2 = newNode(100); root1->left = newNode(7); root1->right = newNode(15); root1->left->left = newNode(4); root1->left->right = newNode(9); root1->right->right = newNode(20); root2->left = newNode(70); root2->right = newNode(150); root2->left->left = newNode(40); root2->left->right = newNode(90); root2->right->right = newNode(200); if (isSameStructure(root1, root2)) printf("Both trees have same structure"); else printf("Trees do not have same structure"); return 0; } |
Both trees have same structure
Recommended Posts:
- Check if two trees are Mirror | Set 2
- Check if two trees are Mirror
- Check if all levels of two trees are anagrams or not
- Iterative function to check if two trees are identical
- Check if leaf traversal of two Binary Trees is same?
- Iterative method to check if two trees are mirror of each other
- Check if two trees are mirror of each other using level order traversal
- AA Trees | Set 1 (Introduction)
- Advantages of Trie Data Structure
- Applications of tree data structure
- Enumeration of Binary Trees
- Dynamic Programming on Trees | Set-1
- Some Basic Theorems on Trees
- Dynamic Programming on Trees | Set 2
- Disjoint Set Union on trees | Set 2
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.



