03. Check if Tree is Isomorphic
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To check if two trees are isomorphic, let's recursively compare the following:
If both trees are empty, they are isomorphic so return true (our base case).
If both trees are not empty and their root nodes have the same
value, we have two possibilities:
Trees are isomorphic when (node's children were not flipped):
The left subtrees of both trees are isomorphic.
The right subtrees of both trees are isomorphic.
Trees are isomorphic when (node's children were flipped):
The left subtree of the first tree is isomorphic to the right subtree of the second tree.
The right subtree of the first tree is isomorphic to the left subtree of the second tree.
If none of the above possibilities meet our criteria, then return false.
Time Complexity: The algorithm visits every node in both trees once, so the time complexity is O(N)
, where N
is the number of nodes in the tree.
Auxiliary Space Complexity: The recursion stack can go as deep as the height of the tree, so the space complexity is O(H)
, where H
is the height of the tree.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.