Given two binary trees node0 and node1, return a merge of the two trees where each value is equal to the sum of the values of the corresponding nodes of the input trees. If only one input tree has a node in a given position, the corresponding node in the new tree should match that input node.
For example, given
0 7
/ \ / \
3 2 5 1
/
3
Return
7
/ \
8 3
/
3
Constraints
n ≤ 100,000 where n is the number of nodes in node0
m ≤ 100,000 where m is the number of nodes in node1
We are performing an in-order traversal (note that pre-order and post-order work as well).
We perform this on both trees at the same time, and add the nodes at each level.
By performing it at the same same, we are traversing the trees in the exact same way which means all we need to do is add the values of the nodes.Implementation
Our basecases are when either of the nodes are none. If they are, we return the other node.