Merge Two Binary Quota Trees by Summing Overlapping Nodes

Last updated: November 29, 2026

Quick Overview

Two teams' storage quotas are each laid out as a binary tree of the same hierarchy shape where present. Merge them into one tree where overlapping nodes sum their values and non-overlapping nodes are kept as they are.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Two teams' storage quotas are each laid out as a binary tree of the same hierarchy shape where present. Merge them into one tree where overlapping nodes sum their values and non-overlapping nodes are kept as they are.

A quick online assessment style question that tests whether you can recurse on two trees at once and reuse subtrees safely. Google graders also look at whether you discuss mutating inputs versus building a fresh tree.

What the Interviewer Expects
  • Recurse on pairs of nodes
  • Reuse the non-null subtree when the other side is null
  • Explain the aliasing trade-off of reusing input nodes
  • Give O(min(n, m)) time for the overlap
  • Offer an iterative version on request
Key Topics to Cover
binary trees
recursion
tree merging
aliasing
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Produce a merged tree without sharing any nodes with the inputs.
  • How would you do it iteratively?
  • What if quotas merge by max instead of sum?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: roots a and b. Output: root of the merged tree. Where both trees have a node at the same position, the merged node has the sum of values. Where only one has a node, that node and its subtre...

High-Level Design

Recurse on positions. If one side is None, return the other side's subtree directly: nothing below it overlaps, so there is no work to do. Otherwise create a new node with the sum and recurse for the ...


Submit Your Answer
Markdown supported