Check if Tree is Isomorphic
A medium Binary Trees problem included in Love Babbar 450. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Binary Trees
- Sheets
- 1
- Core for
- 4 roles
- Platform
- GeeksforGeeks
The problem
Given two binary trees, check whether they are isomorphic. Two trees are isomorphic if one can be obtained from the other by swapping the left and right children of some nodes. The structure may differ but the trees should match after such swaps.
Example 1
- Input
- root1=[1,2,3], root2=[1,3,2]
- Output
- true
- Why
- Swapping the children of the root in root2 makes it identical to root1.
Example 2
- Input
- root1=[1,2,3], root2=[1,2,3]
- Output
- true
- Why
- Identical trees are trivially isomorphic.
Example 3
- Input
- root1=[1,2,3,4,5], root2=[1,2,3,null,null,5,4]
- Output
- false
- Why
- Node 2 in root1 has two children but node 2 in root2 has no left child, making them structurally different even with swaps.
Constraints
- The number of nodes in both trees is in the range [0, 100].
- -100 <= Node.val <= 100
How to think about it
Updated 2026-09-09At every step, two trees can match in one of two configurations: either their left children correspond to each other and their right children correspond, or their children were swapped so that left matches right and right matches left. Any valid isomorphism must satisfy one of these two branchings.
Approaches, worst first
Canonical tree string encoding
time O(n log n) · space O(n)
Serialize both trees into canonical strings where child subtrees are sorted alphabetically before being appended to the parent representation. Compare the two canonical strings for equality.
Branching recursive checkWrite this one
time O(min(4^h, 2^n)) · space O(h)
If both null, return true; if one null or values differ, return false. Otherwise, check `(isIsomorphic(l1, l2) && isIsomorphic(r1, r2)) || (isIsomorphic(l1, r2) && isIsomorphic(r1, l2))`. Tests unswapped and swapped configurations directly.
Where people lose marks · 3
- Only testing swapped children: identical subtrees without swaps must also evaluate to true.
- Comparing node values before checking if either node pointer is null triggers null dereference errors.
- Assuming trees with the same multiset of values are isomorphic; structural subtree groupings must be strictly preserved under child permutations.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
What each operation costs
| Operation | Time |
|---|---|
| traverse all nodes using recursion or queue | O(n) |
| search for an arbitrary value in an unordered tree | O(n) |
| call stack memory on balanced tree | O(log n) |
| call stack memory on skewed tree | O(n) |
What usually goes wrong with Binary Trees
- Dereferencing left or right child pointers without checking if the current node is null, throwing null pointer errors on empty trees or leaf nodes.
- Defining a leaf node incorrectly by stopping when either child is null instead of checking that both left and right children are simultaneously null.
- Computing tree diameter by taking left height plus right height inside a recursive helper without updating a global maximum across every visited node.
Which roles need this problem
Binary Trees is a core topic for these 4 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 5 more roles, including Full-Stack Developer, Android Developer, iOS Developer.
Companies that have asked it
Tags taken from the problem's own GeeksforGeeks page — not a copied list.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Binary Trees problems
Problem set and role mapping as of .