164. How do you represent a generic (n-ary) tree where nodes can have any number of children?
MediumTrees
Technical
Show model answer
Rather than a variable-length child array, each node stores two pointers: `firstChild` and `nextSibling`. A node's children form a linked list starting at its first child and following sibling links. This fixed-size node makes the structure simple and memory-predictable.
Viewed sideways, this is a binary tree (left = first child, right = next sibling), so binary-tree algorithms adapt directly. The trade-off is that reaching the kth child costs O(k) instead of O(1). A child array is better when you need random access to children.