Combine N-ary Trees
Microsoft interview questions · Hard · Coding round · 8 tests
You are given two N-ary organization charts. Each node has a unique department
key, a numeric headcount, and a list of child departments.
Merge the trees with the following rules:
- If one node is
null, return a deep copy of the other node. - When both nodes exist for the same department, the merged node keeps that department's
keyand uses tree B'sheadcount. - Children are merged by key, not by index.
- For child order, keep tree A's children in their original order. If a child with the same key exists in tree B, merge them recursively in that position. After processing all tree A children, append any tree-B-only children in their original order.
Keys that appear in only one tree
Run your code with Ctrl+Enter to see results.