Merge Two Binary Trees

Easy · Trees · about 10 minutes

Asked at Amazon, Meta, Google, Microsoft.

Merge two binary trees by overlaying them: where both have a node, the merged node's value is the sum; otherwise use whichever node exists. Return the merged tree.

Related guides