Dsa
Binary Search Tree
Warm-up
Count Nodes in a BST
You are given the root of a binary search tree (BST). Your task is to count the number of nodes in the tree. Implement the function `count_nodes(root)` where `root` is the root node of the BST. Input and output are in the following format:
Input:
The first line contains an integer N, the number of nodes in the BST.
The following N lines each contain an integer representing the value of the node. Each node value is unique.
Output:
A single integer representing the number of nodes in the BST.
Example:
Input:
3
2
1
3
Output:
3
Key concepts
binary_search_treecountingrecursion
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