Merge neighbor sums from many binary trees into the last
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Snowflake. It tests binary tree, tree traversal and recursion.
You are given a list trees of binary trees (each given by its root, possibly empty). The neighbor sum of a node is its own value plus the values of its left and right children, where a missing child counts as 0.
Treat every tree except the last one as a source. The last tree is the target. Process the sources from left to right. For each node in a source, find the node at the same position in the target. A position is the path from the root: the root, then left or right at each step.
- If the target has a node there, add the source node's neighbor sum to its value.
- If the target has no node there, create one whose value is the neighbor sum, and attach it at that position.
Always compute neighbor sums from the source trees' original values. Do not modify the source trees. Return the root of the updated target. Return None if trees is empty, or if every tree is empty.
def copy_neighbor_sums(trees: list[Optional[TreeNode]]) -> Optional[TreeNode]: ...
Constraints
- 0 <= len(trees) <= 10^3
- Up to 10^5 nodes in total; node values are integers (they may be negative)
- Trees can be very unbalanced (depth up to 10^5)
Example 1 (trees written in level order, None = missing)
trees = [[2, 4, 1], [10, 1]]
The source root's neighbor sum is 2 + 4 + 1 = 7, so the target root becomes 17. The left node's neighbor sum is 4, so the target's left node becomes 5. The target has no right child, so a node with value 1 is created.
Result: [17, 5, 1].
Example 2
trees = [[1, 2], [3], [0]]
Each source adds 3 to the root (1 + 2 + 0, then 3 + 0 + 0), so the root becomes 6. The first source's left child creates a node with value 2.
Result: [6, 2].
Clarifying questions
- If the target tree is empty, should the first source create its root?
- If
treeshas one tree, is it returned unchanged (there are no sources)? - Should an empty source tree be skipped?
- Are the source trees' original values used even when the same node objects appear in more than one tree?
What a strong answer covers
- For each source, walks it with an explicit stack of
(source_node, target_parent, side)so deep trees don't overflow the call stack. - Computes each source node's neighbor sum from its own value and its children's values, never from the target, so sources are never changed and the order of updates doesn't matter.
- Finds or creates the matching target node: adds the neighbor sum when it exists, or attaches a new node with that value at the right side.
- Runs in O(total nodes) time and O(depth) extra space.
- Tests: both examples, an empty list, all trees empty, an empty target with a non-empty source, a very deep chain, negative values, and a check that the source trees are unchanged.
Follow-ups
- Return a new target tree and leave every input unchanged.
- Merge with a different combine rule, such as the maximum instead of the sum.
- Trees are n-ary, with positions given by child index.
- Process the sources in parallel, and say what has to be synchronized.
Where it's asked
What it tests
binary tree · tree traversal · recursion
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
