Dsa
Trees
Medium
Find All Leaf Nodes
Given a binary tree, return a list of all the leaf nodes in the tree. A leaf node is a node that does not have any children. You should print the leaf nodes in sorted order.
Input:
- The first line contains an integer n, the number of nodes in the tree.
- The next n lines describe the tree in the format 'parent child direction', where direction can be 'L' (left) or 'R' (right).
Output:
- A list of integers representing the values of the leaf nodes, sorted in ascending order.
Example Input:
5
1 2 L
1 3 R
2 4 L
2 5 R
3 6 L
Example Output:
[4, 5, 6]
Key concepts
treesleaf nodesdepth-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