Dsa
Binary Search Tree
Warm-up
Count Unique Values in BST
You are given the root of a binary search tree (BST). Your task is to count how many unique values exist in the BST. A value is unique if it appears exactly once in the tree.
Input Format:
- The first line contains an integer n, the number of nodes in the BST.
- The next n lines each contain a single integer, which represents the value of the node inserted into the BST.
Output Format:
- Print a single integer: the count of unique values in the BST.
Example:
Input:
7
5
3
7
3
8
5
9
Output:
3
(Unique values are 7, 8, and 9)
Key concepts
binary_search_treecounting
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