FastPrepMerge Binary Trees by Corresponding Sums
Problems/Merge Binary Trees by Corresponding Sums

Merge Binary Trees by Corresponding Sums

AMD logoAMD● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asks to create a binary tree by summing corresponding nodes from two trees. Missing-node behavior, mutation/reuse permission, bounds and interface are authored; approximate source match90%.

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) → TreeNode

Examples

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.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public TreeNode mergeTrees(TreeNode first, TreeNode second) {
    // Write your code here.
}
Checking account…