Pseudocode to compare two trees
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Comparing two trees is simple only after you define what "same" means. In many problems, two trees are considered equal only if they have the same structure and the same value at every corresponding node. Once that rule is clear, the core algorithm is a recursive walk that compares node pairs in lockstep.
Start with the Equality Definition
Before writing pseudocode, decide whether equality means:
- same shape and same values
- same values even if child order changes
- same leaf sequence only
- same subtree below a chosen node
The standard case is strict equality: both trees must have the same node value in the same position with the same child structure.
Recursive Pseudocode for a Binary Tree
For binary trees, the classic algorithm compares the current nodes, then recurses into left and right children.
This works because every mismatch falls into one of three categories:
- one node exists where the other does not
- both nodes exist but their values differ
- one of the corresponding subtrees differs
Translate the Pseudocode into Real Code
The Python version is almost a direct translation.
That is the version to start with unless tree depth is so large that recursion becomes a problem.
Iterative Version with an Explicit Stack
If you want to avoid deep recursion, the same comparison can be written with a stack.
This is logically the same comparison. The only difference is that you manage the traversal state yourself instead of relying on the call stack.
Adapting the Idea to N-ary Trees
If each node has a list of children, compare child counts first, then compare the children pairwise in order.
This assumes child order matters. If child order does not matter, the problem is more complex because you need a way to match children independent of position.
Common Pitfalls
The most common mistake is comparing values without comparing structure. Two trees can contain the same values and still be different if the nodes are arranged differently.
Another issue is forgetting the null base cases. The algorithm must handle three possibilities at each position: both nodes missing, only one missing, or both present.
Deep recursion is another concern. A skewed tree can cause recursion depth problems in some languages, so an iterative version may be safer for unbalanced inputs.
Finally, define the equality rule before coding. If one person assumes child order matters and another assumes it does not, both implementations can look reasonable while solving different problems.
Summary
- Tree comparison starts with a precise definition of equality.
- For strict equality, compare null state, node value, and corresponding subtrees.
- Recursive pseudocode is the simplest correct solution for binary trees.
- Use an explicit stack if recursion depth may become an issue.
- For n-ary trees, compare child counts first and then compare children in order.
Related reading
- Push_swap sorting 50000 numbers with two rotatable stacks and a limited set of operations
- Puzzle Find largest rectangle maximal rectangle problem
- Puzzle Need an example of a complicated equivalence relation / partitioning that disallows sorting and/or hashing
- Pyramids dynamic programming
- Publish multiple messages to RabbitMQ from a file
- push_back vs emplace_back
- Python - Algorithm find time slots
- python - prefix sum algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.