Dsa
Trees
Easy
All Paths in Binary Tree
Given a binary tree, return all root-to-leaf paths in the tree as a list of strings. Each path should be represented as a comma-separated list of node values.
Input format:
- The first line contains an integer N, the number of nodes in the tree.
- The following N lines contain integers representing each node's value in level-order traversal. Use -1 to indicate 'null' nodes.
Output format:
- Print each path in a new line, sorted lexicographically.
Example:
Input:
5
1
2
3
-1
-1
4
5
Output:
1,2
1,3,5
1,3,4
Key concepts
treespathsrecursion
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