The problem. You are given two binary trees a and b. Imagine laying one on top of the other. Where both trees have a node, the merged node's value is the sum of the two. Where only one tree has a node, that node (with everything under it) is used as it is. Return the merged tree.
Input: a = [1, 3, 2, 5], b = [2, 1, 3, null, 4, null, 7]
Output: [3, 4, 5, 5, 4, null, 7]
Input: a = [1], b = [1, 2] Output: [2, 2]The two trees must be walked in step: the same position in both at once — root with root, left child with left child. It's the Same Tree walk, combining instead of comparing.
Give every position in a tree a number, like the slots of a heap: the root is 0 and the children of slot i are 2i + 1 and 2i + 2. Walk both trees, adding each value into its slot. Then build a new tree from the filled slots.
function mergeTrees(a, b) { const slots = new Map(); // position -> merged value function spread(node, i) { if (node === null) return; slots.set(i, (slots.get(i) ?? 0) + node.val); spread(node.left, 2 * i + 1); // children of slot i: 2i+1 and 2i+2 spread(node.right, 2 * i + 2); } spread(a, 0); spread(b, 0); function build(i) { if (!slots.has(i)) return null; return new TreeNode(slots.get(i), build(2 * i + 1), build(2 * i + 2)); } return build(0);}Time O(n₁ + n₂), space O(n₁ + n₂) for the map and a whole new tree — and a hidden flaw: slot numbers double with every level. In a plain array that's 2^h slots; even as map keys, a tree deeper than about 62 levels overflows a 64-bit integer. Fine for tidy trees, broken for deep ones.
Skip the numbering — just recurse on both trees at once. At each position make a new node holding the sum (treating a missing node as 0), and recurse on the two left children and the two right children.
function mergeTrees(a, b) { if (a === null && b === null) return null; const node = new TreeNode((a ? a.val : 0) + (b ? b.val : 0)); // a fresh node node.left = mergeTrees(a ? a.left : null, b ? b.left : null); node.right = mergeTrees(a ? a.right : null, b ? b.right : null); return node;}Time O(n₁ + n₂), space O(n₁ + n₂) for the new tree, plus O(h) recursion. Correct for any depth — but it still copies parts that only one tree has, node by node.
Two observations make it faster. If only one tree has a node at some position, its entire subtree can be returned as it is — no need to walk or copy it. And where both have a node, add b's value into `a`'s node instead of making a new one. Now the work is proportional only to the overlap of the two trees.
Count the visited positions: only where both trees have a node.
Time O(min(n₁, n₂)) — only overlapping positions are visited — space O(h) for the recursion and no new nodes. The trade-off: the result shares nodes with both inputs and a is modified. If the caller still needs the originals, use the optimal version.
O(n₁ + n₂)Each node of both trees is numbered and stored once.
O(n₁ + n₂)The map and a whole new tree; slot numbers also double each level.
a = [1, 2], b = [1, null, 3]: only the roots are visited.a.val and b.val changes.O(n₁ + n₂)Every node of both trees is visited and copied once.
O(n₁ + n₂)The new tree, plus O(h) recursion.
O(min(n₁, n₂))Only positions both trees have are visited; the rest is attached whole.
O(h)Just the recursion: no new nodes.