Merge Two Binary Trees
Updated
Problem statement
You are given the roots of two binary trees, root1 and root2. Merge them into one binary tree and return its root.
Picture one tree laid on top of the other so that the two roots line up. Below that, the left children line up with each other, and so do the right children. At every position:
- If both trees have a node there, the merged tree has one node there, and its value is the sum of the two values.
- If only one tree has a node there, the merged tree uses that node, so the whole subtree below it carries over.
- If neither tree has a node there, the merged tree has none either.
Either tree may be empty. You may create new nodes, or reuse and change the nodes of the input trees.
In the examples, a tree is written in level order, with null for a missing child.
Function
mergeTrees(root1: TreeNode, root2: TreeNode) → TreeNodeExamples
Example 1
root1 = [4,2,7,1]root2 = [1,6,2,null,3,null,8]return = [5,8,9,1,3,null,8]The roots line up: 4 + 1 = 5. Their left children give 2 + 6 = 8 and their right children give 7 + 2 = 9. Below those nodes the trees do not overlap: node 1 comes only from root1, and nodes 3 and 8 come only from root2.
Example 2
root1 = []root2 = [-3,null,4]return = [-3,null,4]root1 is empty, so the merged tree is the same as root2.
Example 3
root1 = [1,2,null,3]root2 = [1,null,2,null,3]return = [2,2,2,3,null,null,3]Only the roots overlap: 1 + 1 = 2. The left side of the merged tree is the whole left subtree of root1, and the right side is the whole right subtree of root2.
Constraints
- The number of nodes in each tree is between
0and2000. -10^4 <= Node.val <= 10^4
More Snowflake Phone Screen questions
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
- Minimum StackPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Sum All Values in a Binary TreeDuolingo · PHONE SCREENDuolingo interview questions
- Sum Root-to-Leaf Binary NumbersOracle · PHONE SCREENOracle interview questions
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
- Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Root-to-Leaf Paths as StringsAdobe · PHONE SCREENAdobe interview questions
- Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
- Binary Tree Preorder TraversalMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
- Range Sum of BSTMeta · PHONE SCREENMeta interview questions