AO
Back

Merge Two N-ary Trees by Node Key

Also reported as Merge N-ary Tree · Merge N-Tree · LeetCode 617 Variant

CodingOnsiteLast reported September 2026Medium Frequency

Problem Overview

The round: five reports, November 2025 to September 2026, in infra and application loops; four of them are onsite coding rounds. One interviewer said it comes from a real LinkedIn problem. A sixth report, an infra phone screen, names only the high-frequency question about merging two trees into one, without details, so it may be this problem. It is LeetCode 617 (Merge Two Binary Trees) moved to N-ary trees: every node has a key, and only nodes with the same key merge. Reported follow-ups: serialize an N-ary tree, and what to do when merges run concurrently. Two graded parts.

The node (given)

class Node:
    def __init__(self, key, value=None, children=None): ...
def show(node): ...   # (key, value, [children...]) as nested tuples, or None

Part 1 — merge two trees by key

def merge(a: Node | None, b: Node | None) -> Node | None: ...
  • Keys are unique among the children of one node.
  • If one tree is None, return the other. Otherwise the two roots must have the same key; the merged root has that key.
  • Merge level by level. Two nodes merge when they have the same key and the same parent in the merged tree. A key that appears at a different depth, or under a different parent, is a different node.
  • A merged node takes b's value, and its children are merged the same way.
  • A child that only one tree has is carried over with its whole subtree.
  • Child order: a's children in a's order, then b's children whose keys a does not have, in b's order.
  • You may build new nodes or reuse the input nodes; the tests only read the result through show.

Part 2 — serialize the tree

def serialize(root: Node | None) -> str: ...
def deserialize(data: str) -> Node | None: ...
  • deserialize(serialize(t)) must rebuild the same keys, values, child order and shape. The format is yours.
  • Keys are strings and may contain any character, including commas, brackets, quotes and spaces. Values are ints or None. An empty tree round-trips to None.

Clarify first. One report says to ask about corner cases such as roots with different keys. Here that raises ValueError (not tested). The value rule (b wins), the child order and returning the other tree when one side is empty are not in the reports either; ask what the interviewer wants.

Notes: the key on every node, merging only equal keys, a DFS solution, the different-roots corner case, the N-ary version of LeetCode 617, the serialize follow-up and the concurrency follow-up are from the reports. One report links an outside page about overwrite rules that we cannot see in the thread. Ours: the names, the show helper, b's value winning, the child order, returning the other tree when one is None, the per-level rule stated above, the key and value types in Part 2, and every test. The concurrency follow-up is discussed under the solution, not graded.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Concurrency · 1Trade-off discussion · 2
Concurrency

What happens when merges run concurrently?

Probes for: after the merge; the candidate answered MVCC or locks
Trade-off discussion

Serialize an N-ary tree.

Probes for: after the merge

Ask about corner cases first, such as two roots with different keys.

Probes for: before coding

What Reports Emphasize

What the reports say: LeetCode 617 moved to N-ary trees, where every node has a key and only nodes with the same key merge; DFS solves it. Four of the reports are onsite coding rounds, and one interviewer said it comes from a real LinkedIn problem; a phone-screen report that names only merging two trees may be the same problem. Ask about corner cases first, such as roots with different keys. Follow-ups were concurrency (MVCC or locks was the candidate's answer) and serializing the tree.

Common mistakes (ours, not reported): Pairing children by position; Comparing every pair of children; Dropping b's new children; A serialization without the shape.

Edge cases to test (ours): An empty tree on either side; One key at two depths; Disjoint children; Keys with commas.

Practice

Write your own against 10 test cases, or read the worked solution — approach, complexity, and code that runs.
Did this match your interview?

If you have had this round, tell us how close this page was. It is how we keep the pages accurate.

Prepping for LinkedIn with friends?

Send them this page. It is free to read, no account needed.

More LinkedIn Questions

Free preview

Every question in the LinkedIn catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 90 LinkedIn questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
LinkedIn · Coding · Reported 5× across candidate reports
Is this helpful?