FastPrepMerge Two Binary Trees
Problems/Merge Two Binary Trees

Merge Two Binary Trees

Snowflake logoSnowflake● EasyINTERNPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Snowflake technical phone screen for a software engineer intern role: the second of two coding rounds held one after the other, which the author solved. The report names the question only as Merge Two Binary Trees, a well-known exercise, with no statement, examples or limits.

We used the standard form of that task: where both trees have a node the values add, and where only one has a node the merged tree keeps it with its subtree. We chose the limits (up to 2000 nodes in each tree, values from -10^4 to 10^4) and the examples.

Our version is about 90% the same as the reported question.

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

Examples

Example 1

root1 = [4,2,7,1]root2 = [1,6,2,null,3,null,8]return = [5,8,9,1,3,null,8]
root1
4217
root2
16328
return
581398

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
empty
root2
-34
→
return
-34

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]
root1
123
root2
123
return
22323

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 0 and 2000.
  • -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

Snowflake interview experiences

  • Snowflake AI Coding Interview, Experienced Hires: 3 Experiences
All Snowflake interview questionsSee Snowflake hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        // Write your code here.
    }
}
Checking account…