In left-child right-sibling encoding, what does preorder on the encoded binary tree correspond to?
answer
- what the two pointers actually mean here
- left means down, right means across
- unroll the sibling chain at one node
- the walk emits a node then its whole subtree
- the original order, unchanged
basics
~20 sIt corresponds exactly to preorder on the original n-ary tree. Left-child right-sibling gives every node two fixed pointers, first child and next sibling, and a preorder walk of that binary shape emits the original nodes in the original order.
solid answer
~50 sIn left-child right-sibling encoding, a node's left pointer is its first child and its right pointer is its next sibling, so any n-ary tree becomes a binary tree with two fixed pointers per node and no variable-length list. The transform is lossless and reversible: child order is preserved by the sibling chain. Preorder on the encoding — node, then left, then right — emits node, then that node's whole subtree, then its siblings, which is precisely the original n-ary preorder. The less obvious correspondence is that an *inorder* walk of the encoding yields the original's postorder, since inorder visits the first-child chain, then the node, then the sibling chain. What the encoding does not preserve is depth: a root with `k` children becomes a right chain of length `k`, so a shallow wide tree can encode as a deep skewed binary tree, and level-order on the encoding has no relation to the original's tiers.
go deeper
Be ready to say what the two pointers mean: left is the first child, right is the next sibling. Knowing that the transform is reversible and preserves child order is enough at this level.
Explain the preorder correspondence by unrolling the sibling chain at one node, and be able to reconstruct a node's children from the encoding by following left once and then right repeatedly.
Show the tradeoffs: uniform two-pointer nodes and no per-node collection, paid for with linear-time child indexing, no cheap fanout count, and a structure whose depth grows with preceding siblings rather than matching the original height.
Own the adoption decision — the encoding is worth it when a fixed-size node across millions of nodes is the binding constraint, and worth refusing when the team must read and maintain it, since misreading the sibling pointer as a child is a recurring and expensive bug.
## The encoding Left-child right-sibling turns an arbitrary n-ary tree into a binary tree without changing the node count and without losing any information. Each node keeps exactly two references: - **left** = the node's *first child* - **right** = the node's *next sibling* A node with no children has a null left pointer; the last child in a sibling group has a null right pointer. The root's right pointer is null in a single tree (in a forest, the right chain of the roots is how you encode several trees as one binary tree — a small bonus of the representation). The transform is reversible: to recover a node's children, follow left once and then chase right pointers until null. Sibling order is preserved by the order of that chain, so nothing about the original model is lost. ## The preorder correspondence Binary preorder is: visit the node, walk left, walk right. Substituting the meanings of the pointers, that reads: visit the node, walk its first child's chain, then walk its own next sibling's chain. Unrolling the recursion at a node with children `c1 … ck`, the left walk emits `c1`'s entire subtree, then — because `c2` hangs off `c1`'s right pointer — `c2`'s entire subtree, and so on through `ck`. So the emitted sequence is: node, subtree of `c1`, subtree of `c2`, …, subtree of `ck`. That is exactly the n-ary preorder definition. This is why the encoding is genuinely useful and not just a curiosity: code written for the binary shape, with two fixed pointers and no collection allocation per node, produces the correct traversal for a tree of unbounded fanout. ## The less obvious one: inorder gives postorder Binary inorder is: walk left, visit the node, walk right. At a node with children `c1 … ck`, the left walk covers the sibling chain `c1 … ck` and, inductively, emits each one's *postorder*; then the node itself is visited; then the right walk continues with the node's own siblings. So the encoding's inorder emits `post(c1), …, post(ck), node` — the n-ary postorder. That correspondence is a satisfying thing to derive out loud and a fair senior-level probe. No such tidy story exists for level-order. The encoding's binary depth is not the original depth, so the encoding's levels mix nodes from many original tiers. If you need tiers, traverse the original shape or carry depth explicitly. ## What the encoding costs | Operation | Children-list node | Left-child right-sibling | |---|---|---| | Reach the i-th child | direct indexed access | follow left, then i-1 right links: O(i) | | Number of children | already known | O(k) walk, unless stored | | Add a child at the end | append to the list | O(k) walk to the chain's tail, or keep a tail pointer | | Add a child at the front | may shift elements | O(1) pointer splice | | Node memory | two-pointer header plus a growable collection | exactly two references, uniform | | Depth of the structure | equals the original height | original height plus preceding-sibling counts | The last row is the one people miss. Depth in the encoding grows with the number of siblings preceding a node along its path, so a two-level tree with a root and a thousand children encodes as a binary tree of depth one thousand. Anything whose cost tracks the binary structure's height — a recursive walk, a path-following operation — behaves very differently on the encoding than on the original. ## When the encoding is the right call It earns its place where a *uniform, fixed-size node* is worth more than random child access: nodes packed into a flat block with no per-node collection allocation, structures written into a compact file format, or an environment where allocating a growable list per node is the dominant cost. Two references per node is also a predictable memory footprint across millions of nodes, whereas a per-node collection carries its own header and slack. It is the wrong call when code frequently asks for "the third child", "how many children does this node have", or "the tiers of this hierarchy" — all cheap on a children list and all linear-in-fanout or worse on the encoding. It also costs readability: engineers reading `node.right` and thinking "second child" is a real and recurring bug, so a team adopting it should rename the fields to `firstChild` and `nextSibling` and let the two-pointer layout be an implementation detail rather than the mental model. ## Common misconceptions to avoid That the right pointer is a second child — it is a sibling, and reading it as a child scrambles the structure. That the encoded binary tree is a search tree — there is no ordering invariant on keys at all; the shape is a representation, not an index. That the encoding loses child order — it does not; the sibling chain *is* the order. And that a walk of the encoding costs the same as on the original in every respect — total traversal is still O(n), but per-node child access and structural depth are not comparable.
- What does the encoding cost you when code needs a node's third child?You follow the left pointer to the first child and then two right links, so access is O(i) for the i-th child instead of direct indexing, and there is no O(1) child count unless you store one. Any code that indexes children or reports fanout gets slower in proportion to the fanout it is walking past.
- Does the encoded binary tree have the same height as the original tree?No. A node's depth in the encoding is its original depth plus the number of siblings preceding it along its path, so a root with a thousand children becomes a right chain of a thousand. A shallow wide hierarchy can encode as a very deep skewed binary tree, which is why anything whose cost tracks structural height behaves differently on the encoding.
- Which original traversal does an inorder walk of the encoding produce?Postorder. Inorder visits the left chain first, which is the node's children in order and inductively each of their postorders, then the node, then the sibling chain. That yields all children's postorders followed by the node, which is the n-ary postorder definition.
It is a family tree drawn with one line down to the eldest child and one line across to the next-born, so every person needs exactly two lines no matter how large the family.
saying these in an interview costs you the question
- Reads the right pointer as a second child
- Claims the encoding loses the order of children
- Says the encoded binary tree is a search tree
- Assumes the encoding preserves depth or level boundaries
- Expects indexed child access to stay constant-time