Flatten Binary Tree to Linked List (In-place)

Data Structures
Medium
Amazon
88.6K views

Flatten a binary tree into a single singly linked list in-place. The list should use the right pointer and the order should be the same as preorder traversal.

Why Interviewers Ask This

Amazon interviewers ask this to evaluate your mastery of pointer manipulation and in-place algorithmic optimization. They specifically test if you can transform complex tree structures into linear lists without allocating extra memory, a skill critical for high-performance systems. This problem also assesses your ability to handle recursive logic while managing edge cases like null pointers and ensuring the right pointer correctly links nodes in preorder sequence.

How to Answer This Question

1. Clarify Requirements: Confirm the output must be preorder traversal using only the 'right' pointer for the next node and 'left' set to null. Ask about constraints on memory usage. 2. Choose Strategy: Propose two approaches. First, a simple recursion with O(N) space due to the call stack. Second, an optimal Morris Traversal or iterative approach achieving O(1) space, which Amazon values for efficiency. 3. Walk Through Logic: For the optimal solution, explain how to find the inorder predecessor (the rightmost node in the left subtree). If its right pointer is null, redirect it to the current node's right child, then move the current node's right to its left child, and clear the left pointer. 4. Handle Edge Cases: Explicitly mention handling empty trees, single-node trees, and nodes with only right children. 5. Code Implementation: Write clean code with comments explaining the pointer reassignment steps, emphasizing the in-place nature of the transformation.

Key Points to Cover

  • Demonstrating understanding of preorder traversal mechanics before coding
  • Proposing an O(1) space solution rather than relying on recursion stack
  • Explicitly handling the re-linking of the rightmost node in the left subtree
  • Ensuring the left pointer is cleared to prevent cycles or memory leaks
  • Discussing edge cases like empty trees or skewed trees proactively

Sample Answer

To flatten this binary tree in-place, I will first clarify that we need the result to follow preorder traversal order using only the right pointers. A naive recursive solution would work but uses O(H) stack space. Given Amazon's focus on resource efficiency, I prefer an iterative approach similar to Morris Traversal to achieve O(1) space complexity. The core logic involves iterating through the tree. For each node, if it has a left child, we locate the rightmost node in that left subtree, which is the immediate predecessor in the flattened list. We then attach the original right subtree of the current node to this predecessor's right pointer. Next, we move the entire left subtree to the right side of the current node and set the left pointer to null. We then proceed to the new right child. For example, consider a root with value 1, left child 2, and right child 3. Node 2 has no children. We find the predecessor of 2, which is itself since it has no left child? No, wait. If 2 had a left child 4, 4 would be the predecessor. In our case, 2 has no left child, so we just move to 3. The algorithm ensures that every node's left pointer becomes null and the right pointer points to the next node in preorder. This avoids recursion depth issues and meets the strict memory constraints often required in backend engineering roles.

Common Mistakes to Avoid

  • Allocating a new array or list to store the result, violating the in-place constraint
  • Confusing preorder with inorder or postorder traversal sequences during pointer linking
  • Failing to update the right pointer of the predecessor node before moving the left subtree
  • Not clearing the left pointer after moving the subtree, leaving dangling references

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 73 Amazon questions