Description

Given two binary trees root1 and root2, merge them into a new binary tree. For overlapping nodes, add the values. For non-overlapping, use the existing node.

Examples

Input:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
Output:[3,4,5,5,4,null,7]
Explanation:

Merged values.

Input:root1 = [1], root2 = [2,1,3,null,4,null,7]
Output:[1]
Explanation:

Merging a single-node tree [1] with a larger tree. The root values are summed where nodes overlap.

Input:root1 = [4,2,6,1,3], root2 = [3,1,5,null,null,4,7]
Output:[7,3,11,1,3,4,7]
Explanation:

Both trees have multiple levels with overlapping nodes. Root nodes merge (4+3=7), left children merge (2+1=3), right children merge (6+5=11). Left subtree keeps node 1 from root1 and node 3 from root1, while right subtree gets node 4 from root2 and node 7 from root2.

Constraints

  • 0 ≤ nodes ≤ 2000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!