Introduction
This blog will discuss the solution to the problem to find the maximum count of duplicate nodes in a binary search tree, here we are given a binary search tree. It has duplicate nodes, and we need to find the maximum count of duplicate nodes in this binary search tree. If there is more than one node with a maximum count, we can print any one of them.
Sample Examples
Input: The given bst:

Output: The node with a maximum count of duplicates is: 10
Input: The given bst:

Output: The node with a maximum count of duplicates is: 4
Brute Force Approach
The brute force approach of this problem to find the maximum count of duplicate nodes in a Binary Search Tree is to hash all the node values of the bst in the map.
After that, we will traverse the map and store the node with the maximum hash value in a variable because the hash value equals the count of nodes in the bst.
Algorithm
- We will make a function inorder() which will take one argument, i.e., the tree's root. We will maintain a map and hash all the elements of the bst into the map while traversing the bst.
- Now in the main code, we will traverse the map and maintain a variable to store the Node with the maximum count of duplicates.
- After the map iteration is over, we will print our answer.
Code in C++
// C++ implementation to find the maximum count of duplicate nodes in a binary search tree
#include <bits/stdc++.h>
using namespace std;
// Structure of each node of BST
struct Node
{
int key;
struct Node *left, *right;
};
// Function to create a new BST node
Node *TreeNode(int data)
{
Node *temp = new Node();
temp->key = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert node in bst
struct Node *insert(struct Node *node, int key)
{
// If the tree is empty, then return a new node
if (node == NULL)
return TreeNode(key);
// Otherwise, recur down the tree
if (key <= node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
// Return the (unchanged) node pointer
return node;
}
// to hash the elements of the bst
map<int, int> m;
// Function to count of duplicate nodes in a bst
void inorder(struct Node *root)
{
// base case
if (!root)
return;
// hashing the current node in the map
m[root->key]++;
inorder(root->left);
inorder(root->right);
}
int main()
{
/* Formation of BST 1
5
/ \
2 7
/\ /\
1 4 6 10
*/
struct Node *root1 = NULL;
root1 = insert(root1, 5);
insert(root1, 2);
insert(root1, 2);
insert(root1, 4);
insert(root1, 7);
insert(root1, 6);
insert(root1, 10);
int x = 7, ans = 0, node = 0;
inorder(root1);
for (auto y : m)
{
// to check for node with maximum count of duplicates
if (ans < y.second)
{
ans = max(ans, y.second);
node = y.first;
}
}
cout << "The node with a maximum count of duplicates is:" << node << endl;
return 0;
}
Output:
The node with a maximum count of duplicates is: 2
Complexity Analysis
Time Complexity: O(N)
Since we are doing inorder traversal of the bst and inorder traversal can be done in O(N), the time complexity is O(N).
Space Complexity: O(N)
Since we are hashing all the nodes in the map, therefore, the space complexity is O(N).





