Dsa
Trees
Medium
Leaf Count in Range
You are given a binary search tree (BST) and need to find the count of leaf nodes whose values fall within a specified range [low, high].
The input format is as follows:
- The first line contains an integer n, the number of nodes in the BST.
- The next n lines each represent a node in the format 'value left right', where 'value' is the integer value of the node, 'left' is the index of the left child node (0-indexed, -1 if none), and 'right' is the index of the right child node (0-indexed, -1 if none).
- The last line contains two integers, low and high (inclusive), which represent the range.
You should output a single integer representing the count of leaf nodes within the specified range.
Example:
Input:
3
10 1 2
5 -1 -1
15 -1 -1
5 15
Output:
2
Key concepts
treesbinary search treerange querycounting
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