flattening on Binary Tree

Last updated: January 17, 2026

Quick Overview

Given a binary tree, flatten it into a linked list in-place following the order of the tree's pre-order traversal. The output should be the root of the modified tree, with all left pointers set to null and the right pointers pointing to the next node in the flattened list.

Grubhub
Coding & Algorithms
Software Engineer
Grubhub
January 17, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

157

11

4,118 solved


Given a binary tree, flatten it into a linked list in-place following the order of the tree's pre-order traversal. The output should be the root of the modified tree, with all left pointers set to null and the right pointers pointing to the next node in the flattened list.

Coding interviews at Grubhub focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Dynamic programming and memoization
Edge cases and input validation
Time and space complexity analysis
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the worst-case input for your solution?
  • Can you solve this in a single pass?
  • How would your solution change if the input was sorted?
  • How would you test this solution thoroughly?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires us to flatten a binary tree into a linked list using pre-order traversal. The underlying pattern here is Depth-First Search (DFS) because we need to explore the tree's nodes deepl...

Approach
  1. Start from the root of the binary tree.
  2. Use a helper function that takes the current node and a reference to the last processed node.
  3. For each node, store the right child temporarily, set t...

Submit Your Answer
Markdown supported

Related Questions