Merge two binary trees by combining overlapping nodes and keeping non-null branches.
You are given two binary trees. Create a new binary tree by merging them together with the following rule:
Return the root of the merged tree.
The merge should preserve the tree structure wherever possible.
root1 and root2.Example 1
Input
root1 = [1,3,2,5] root2 = [2,1,3,null,4,null,7]
Output
[3,4,5,5,4,null,7]
Explanation
The roots overlap, so 1 + 2 = 3. Merge the left children 3 and 1 into 4, the right children 2 and 3 into 5, and continue recursively for lower levels.
Example 2
Input
root1 = [1] root2 = [1,2]
Output
[2,2]
Explanation
The root values add to 2. The left child exists only in the second tree, so it is kept directly.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.