| Topic | Difficulty | Companies |
|---|---|---|
| Binary Tree | EASY | Amazon Microsoft |
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. Write a program to merge them into a new binary tree.
Problem Note
Example
Input:
Tree 1 Tree 2
1 2
/ \ / \
6 2 1 3
/ \ \
5 4 7
Output: Merged tree
3
/ \
7 5
/ \ \
5 4 7
Explanation: The root nodes when merged sum up their values, the new root has value 1 + 2 = 3.
Overlapping nodes have their values sum up like 6 + 1 =7.
The nodes like 5 and 4 are not overlapping, so NOT null value is put in the new binary tree.