Merge Two Binary Trees is a tree traversal problem that checks whether you can combine two recursive structures cleanly. You are given the roots of two binary trees. Your job is to merge them into a single binary tree following a simple rule.
If two nodes overlap at the same position, the merged tree should contain a node whose value is the sum of the two values. If a node exists in one tree but not the other, the merged tree should reuse the existing node and its entire subtree.
In practice, you can think of the two trees as being placed on top of each other. Wherever both trees have a node, you add values. Wherever only one tree has a node, you keep that node as-is.
This problem is common in interviews because it’s a clean way to test recursion, base cases, and how well you reason about structure rather than just values.
Why recursion fits naturally here
A binary tree is a recursive data structure. Every node has a left subtree and a right subtree, and those subtrees are themselves binary trees.
Because the merging rule applies to every corresponding pair of nodes, the most straightforward solution is also recursive. You solve the merge for the current pair of nodes, then merge their left children, then merge their right children.
This “same work at every level” pattern is exactly what recursion is designed for.
The core merging logic
The merge decision at any position comes down to three cases.
If both nodes are null, the merged result at that position is null.
If one node is null and the other is not, the merged result is simply the non-null node. There is nothing to add, and the problem statement says to keep the existing subtree.
If both nodes are present, you create a merged node whose value is the sum of the two node values. Then you merge the left children to form the merged node’s left child, and merge the right children to form the merged node’s right child.
That’s the entire rule, repeated throughout the tree.
Want to explore more coding problem solutions? Check out the Maximum Product of Three Numbers and Top K Frequent Words.
Two ways to think about implementation
Interviewers are usually fine with either of these approaches, as long as you explain it clearly.
One approach is to build a new tree. That means you create new nodes for every overlap and recursively attach merged children. This keeps the original trees unchanged, which can be a nice property when you want to avoid side effects.
The other approach is to merge in place, usually into the first tree. In that version, when both nodes exist, you add the second node’s value into the first node and recursively merge children, reusing nodes as much as possible. This can be more memory efficient because you avoid creating new nodes for overlapping parts.
Conceptually, both approaches follow the same merge rules. The difference is whether you mutate an existing tree or construct a fresh one.
Why the solution is correct
Correctness comes from the fact that each position in the merged tree depends only on the nodes at that same position in the input trees.
If both trees have nodes there, you must add them. If only one does, you must preserve it. Recursion ensures that the same decision rule is applied consistently for left and right subtrees, all the way down until you hit nulls.
Because every node position is handled exactly once, there is no risk of missing nodes or duplicating subtrees. The merge operation respects the structure of both trees while combining values wherever overlap occurs.
Performance in simple terms
The traversal visits each node that exists in either tree, so the runtime is proportional to the total number of nodes across both trees.
Space usage depends on the approach. If you build a new tree, you allocate new nodes for overlaps, which uses more memory. If you merge in place, extra memory is mostly limited to the recursion call stack.
In both cases, the recursion depth is tied to the height of the tree.
Top comments (0)