Practice Real Company Assessment Patterns • Latest 2025–2026 Mock Tests

Programming Exercises

Implement a function to flatten a binary tree into a linked list in-place

Hard

Problem Statement

Implement a function to flatten a binary tree into a linked list in-place

Sample Input

Tree: 1 → 2 → 5 → 3 → 4 → 6

Sample Output

1 2 3 4 5 6 

Limits

Time: 2s · Memory: 256 MB

Code Editor
00:00
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Main { public static TreeNode flattenTree() { // Create the binary tree here // Your code here return null; } }
Log in or sign up to run or submit your code.Log inSign up
Input
Tree: 1 → 2 → 5 → 3 → 4 → 6
Expected Output
1 2 3 4 5 6