Skip to main content
All Questions

Implement merging of two key-value trees by summing values along matching key paths.

Unlock detailed company stats for this questionUpgrade

Each node has a key and a value that is the sum of itself and its children. It is not a binary tree. When merging, absent branches are created and values of existing branches are summed. Keys are unique among the children of a single parent. Only nodes with the same key path from the root merge, so an M under C does not merge with an M under W. Child nodes may be stored in any order. Example: source ROOT:22 (W:2, K:16, C:4) merged into target ROOT:20 (W:5, K:2, R:13) gives ROOT:42 with W:7, K:18, C:4, R:13. Implement TreeNode and MergeTwoTrees::merge(source, target).

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.