Tap here to switch tabs
Problem
Submissions

Problem: Merge Two Binary Trees

Try to solve the Merge Two Binary Trees problem.

Statement

You are given the root nodes of two binary trees, root1 and root2.

Merge the two trees into a single binary tree. When two nodes overlap, their values are summed up to create the new value for the merged node. If a node exists in only one tree at a given position, that non null node is used directly in the merged tree.

Return the root of the merged binary tree.

Note: The merging process begins at the root nodes of both trees.

Constraints:

  • The number of nodes in both trees is in the range [0,2000][0, 2000].

  • −104≤-10^4 \leq Node.val ≤104\leq 10^4

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Merge Two Binary Trees

Try to solve the Merge Two Binary Trees problem.

Statement

You are given the root nodes of two binary trees, root1 and root2.

Merge the two trees into a single binary tree. When two nodes overlap, their values are summed up to create the new value for the merged node. If a node exists in only one tree at a given position, that non null node is used directly in the merged tree.

Return the root of the merged binary tree.

Note: The merging process begins at the root nodes of both trees.

Constraints:

  • The number of nodes in both trees is in the range [0,2000][0, 2000].

  • −104≤-10^4 \leq Node.val ≤104\leq 10^4