Dsa
Trees
Hard
Tree to List Conversion
Given a binary tree, convert it into a list of lists where each list represents the values of the nodes at that depth of the tree. Input consists of multiple lines where the first line contains an integer n (the number of nodes), followed by n lines each containing an integer value for the node. If the tree is empty (n = 0), output should be an empty list. The output should be printed as a single line JSON array of arrays.
Example:
Input:
4
1
2
3
4
Output:
[[1], [2, 3], [4]]
Key concepts
treesrecursiondepth-first-search
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now