KatInterview

Combine Tree Structures with Ordered Name Matching

Trees & GraphsAuto-graded · 20 test cases
HardAuto-gradedTrees & Graphsrecursiontree-traversalhash-table

Last updated

Tests recursive tree traversal with order-sensitive child pairing by name occurrence and careful handling of unmatched nodes.

You are given two tree structures where each node is represented as a dictionary containing three keys:

  • name: a string identifier
  • value: an integer
  • children: a list of child nodes (ordered)

Your task is to combine these trees into a new tree following specific merging rules.

Merging Rules

When combining a node from the first tree with a node from the second tree:

  1. Name and Value: The resulting node takes its name from the first tree and its value from the second tree.

  2. Child Pairing: Children are matched by name based on their order of occurrence. For each distinct name, the first child with that name in tree one pairs with the first child with that name in tree two, the second pairs with the second, and so on. Each matched pair is recursively combined using these same rules.

  3. Result Structure: The merged node's children list is constructed as follows:

    • First, include all children from the first tree in their original order. If a child was paired, replace it with the recursively merged result. If unpaired, include it unchanged along with its entire subtree.
    • Then, append all unpaired children from the second tree in their original order, each with its complete subtree.
  4. Root Handling: The root nodes are always merged together, regardless of whether their names match.

Output Format

Return a new tree where every node has exactly the keys name, value, and children. Leaf nodes have children set to an empty list []. The order of elements in every children list matters.

Constraints

  • Each tree contains between 1 and 10⁴ nodes
  • Maximum tree depth is 500 nodes on the longest root-to-leaf path
  • Node names are non-empty strings of at most 20 lowercase letters and digits
  • Siblings may have duplicate names; names can appear anywhere in either tree
  • Node values are integers where -10⁹ ≤ value ≤ 10⁹
  • The resulting merged tree has fewer than 2 × 10⁴ nodes

Examples

Example 1

Input: combine_trees({'name': 'root', 'value': 1, 'children': [{'name': 'a', 'value': 2, 'children': []}, {'name': 'b', 'value': 3, 'children': []}]}, {'name': 'top', 'value': 10, 'children': [{'name': 'b', 'value': 30, 'children': []}, {'name': 'c', 'value': 40, 'children': []}]})

Output: {'name': 'root', 'value': 10, 'children': [{'name': 'a', 'value': 2, 'children': []}, {'name': 'b', 'value': 30, 'children': []}, {'name': 'c', 'value': 40, 'children': []}]}

Example 2

Input: combine_trees({'name': 'r', 'value': 0, 'children': [{'name': 'x', 'value': 1, 'children': [{'name': 'y', 'value': 2, 'children': []}]}, {'name': 'z', 'value': 3, 'children': []}, {'name': 'x', 'value': 4, 'children': []}]}, {'name': 'r', 'value': 9, 'children': [{'name': 'x', 'value': 5, 'children': [{'name': 'w', 'value': 6, 'children': []}, {'name': 'y', 'value': 7, 'children': []}]}, {'name': 'x', 'value': 8, 'children': []}, {'name': 'x', 'value': 11, 'children': [{'name': 'v', 'value': 12, 'children': []}]}]})

Output: {'name': 'r', 'value': 9, 'children': [{'name': 'x', 'value': 5, 'children': [{'name': 'y', 'value': 7, 'children': []}, {'name': 'w', 'value': 6, 'children': []}]}, {'name': 'z', 'value': 3, 'children': []}, {'name': 'x', 'value': 8, 'children': []}, {'name': 'x', 'value': 11, 'children': [{'name': 'v', 'value': 12, 'children': []}]}]}

Hints

Hint 1

Build an index mapping each name to the positions of children with that name in the second tree

Hint 2

Track which occurrence of each name you've already paired by maintaining a pointer for each distinct name

Hint 3

Mark which children from the second tree have been paired so you know which ones to append at the end