KatInterview

Convert Multi-Level Branching List to Doubly Linked List

Linked ListsAuto-graded · 23 test cases
HardAuto-gradedLinked Listslinked-liststrees-graphsstacks-queues

Last updated

Flatten a tree-like linked structure with main and side links into a single doubly linked sequence using preorder traversal.

You are given a linked structure containing $n$ nodes indexed from $0$ to $n - 1$. Node 0 serves as the entry point. Each node i can have two outgoing pointers:

  • next_node[i]: the next node in the current sequence, or -1 if node i is the last element of its sequence.
  • branch[i]: the head node of an attached side sublist, or -1 if node i does not have a side sublist.

Side sublists can themselves contain further side sublists. The overall structure forms a valid tree-like topology without cycles or shared nodes, starting at node 0.

Your task is to flatten this multi-level structure into a single doubly linked sequence according to the following order:

  1. Every node i must be immediately followed by the complete flattened content of its side branch (if one exists).
  2. Following the side branch, the flattened sequence continues with the remaining nodes that originally followed node i via next_node.

After flattening, all side branches are removed, and nodes are connected exclusively by forward and backward links.

Return the flattened doubly linked list represented as a pair of arrays [forward_links, backward_links], each of length $n$:

  • forward_links[i] is the node that comes immediately after node i in the flattened order (-1 if i is the final node).
  • backward_links[i] is the node that comes immediately before node i in the flattened order (-1 if i is the starting node 0).

Constraints

  • $1 \le n \le 100,000$, where $n = \text{len}(\text{next_node}) = \text{len}(\text{branch})$
  • $-1 \le \text{next_node}[i] \le n - 1$ and $-1 \le \text{branch}[i] \le n - 1$ for all $0 \le i < n$
  • Node 0 has no incoming links. Every other node $1 \le i < n$ is pointed to by exactly one entry across next_node and branch.
  • Every node is reachable from node 0.
  • Nesting depth of branches or long next_node chains can reach up to $n - 1$.
  • The output structure is uniquely determined.

Examples

Example 1

Input: flatten_with_branches([1, 2, -1], [-1, -1, -1])

Output: [[1, 2, -1], [-1, 0, 1]]

Example 2

Input: flatten_with_branches([1, 2, -1, 4, -1, -1], [-1, 3, -1, 5, -1, -1])

Output: [[1, 3, -1, 5, 2, 4], [-1, 0, 4, 1, 5, 3]]

Example 3

Input: flatten_with_branches([1, -1, 3, -1], [-1, 2, -1, -1])

Output: [[1, 2, 3, -1], [-1, 0, 1, 2]]

Hints

Hint 1

View the node structure as a binary tree where 'branch' acts as the left child and 'next_node' acts as the right child.

Hint 2

What tree traversal order visits the root, then the left subtree, then the right subtree?

Hint 3

Because nesting and chain length can reach up to 100,000, recursion might cause a stack overflow in Python. How can you implement this iteratively?