|
|
|
|
|
by pcmonk
3399 days ago
|
|
Generally, "flatten" means the first of those two orders, but I see that the problem as given doesn't disambiguate those. If an interviewer wants to only accept one of those, he should clarify. Incidentally, the first of those is preorder and postorder traversal of the list-of-lists (inorder only exists for binary trees), while the second is none of those. I would describe that difference as depth-first (first) vs breadth-first (second). |
|
In many situations, the order of the leaves doesn't matter at all. In others, it matters a great deal. The question should be clear if it does matter.
Depth-first for breadth-first was not specified. Both preorder and postorder are subclasses of depth-first. Neither preorder nor postorder was specified.
Still, it depends on what you consider a node and a leaf and how you build your tree how much the order matters.
Is a deeper nesting level the child node of a value? Are all values leaves and their parent nodes the nesting level? Are nesting levels collapsed into single nodes or are different nested lists at the same level sibling nodes?
In preorder you're going to get: [], 1, [[]], 2, 3, 4, [[[]]], 5, 6 In postorder: 1, 2, 3, 4, 5, 6, [[[]]], [[]], []Let me explode that initial root this time and make some values nodes if they are followed by an increased nesting level.:
Preorder? 1, [], 2, 3, [], 4, [[]], 5, 6 Postorder? Aah... 2, 3, [], 5, 6, [[]], 4, [], 1So even after you splice out empty nodes, your values are suddenly out of sort order. It's still depth-first.
Or maybe we just don't build the nesting levels into our tree because we don't care. But we still build the tree according to it. We're flattening, after all, and the spec doesn't say we need to retain a nesting level as some attribute of the objects in the flattened list. But what becomes a node vs. a leaf is left as an exercise.
Preorder? 1, 2, 3, 4, 5, 6 Postorder? 2, 3, 5, 6, 4, 1So yeah, it matters. The obvious recursive approach is preorder, especially if you want to maintain sorting. That's what I called "flatten" in my code example earlier. "flatten2" is breadth first as mentioned. Consider this depth-first Perl5 subroutine:
In each level of recursion it's place the children at the beginning of the array (unshift) and the parent to the end of the array (push) even though it's actually considering them each whenever it reaches them.Given my expanded example input of:
This produces output like this, which is definitely the absolute deepest nesting first in the output.: Then there's a true postorder traversal, with an array holding the parent back until after its immediate children.: which provides output as such since it's considering the parent node after all its children every time.: Whereas preorder depth-first keeps the numerical order for this input and the breadth-first traversal gives: as previously shown.So no, preorder and postorder do not necessarily mean the same output just because the input started as a nested list.
A nested list is not simply a list. It is a special representation of a tree. How you consider that tree to be represented by the nested list is important, as is how you traverse it. In fact, in Perl [] is an array reference, which is why this code is testing for references and recursing on them. So it's already traversing an actual tree structure.