Merge Binary Trees by Corresponding Sums
Problem statement
Create a tree over the union of the two input shapes. At a position present in both inputs, use the sum of their node values. At a position present in only one, keep that value and its structure; two missing nodes remain missing. You may reuse and modify input nodes, or allocate a new tree. Only the resulting structure and values are graded.
Function
mergeTrees(first: TreeNode, second: TreeNode) → TreeNodeExamples
Example 1
first = [1,3,2,5]second = [2,1,3,null,4,null,7]return = [3,4,5,5,4,null,7]Overlapping values sum to3at the root,4on its left and5on its right; the remaining5,4 and7come from one input each.
Example 2
first = []second = [2,1,3]return = [2,1,3]An empty first tree contributes no values, so the other tree structure and values remain.
Constraints
0 <= every serialized input tree length <= 400.-1000 <= every non-null input node value <= 1000.- Every input is a valid acyclic binary tree serialized in level order with null missing children; [] denotes an empty tree.