Dsa
Trees
Easy
All Paths from Root to Leaves
Given a binary tree, return all paths from the root to leaves as a list of lists. Each path should be represented as a list of values, and if the tree is empty, return an empty list.
Input format:
- The first line contains an integer N, the number of nodes in the binary tree.
- The next N lines describe the nodes in level order where -1 indicates a null node.
Output format:
- Print all paths where each path is presented on a new line.
Example:
Input:
5
1
2
3
-1
-1
-1
-1
Output:
[1, 2]
[1, 3]
Key concepts
treespathfinding
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