Given a Binary Search Tree (BST) with duplicates, find the node (the most frequently occurred element) in the given BST. If the BST contains two or more such nodes, print any of them.
Note: We cannot use any extra space. (Assume that the implicit stack space incurred due to recursion does not count)
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
- Both the left and right subtrees must also be binary search trees.
Input : Given BST is 6 / \ 5 7 / \ / \ 4 5 7 7 Output : 7 Input : Given BST is 10 / \ 5 12 / \ / \ 5 6 12 16 Output : 5 or 12 We can print any of the two value 5 or 12.
To find the node, we need to find the Inorder Traversal of the BST because its Inorder Traversal will be in sorted order.
So, the idea is to do recursive Inorder traversal and keeping the track of the previous node. If the current node value is equal to the previous value we can increase the current count and if the current count becomes greater than the maximum count, replace the element.
Below is the implementation of the above approach:
node of BST is 7
Time complexity :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Binary Tree to Binary Search Tree Conversion
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Search Tree | Set 1 (Search and Insertion)
- Count the Number of Binary Search Trees present in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree
- Print all even nodes of Binary Search Tree
- Print all odd nodes of Binary Search Tree
- Pre-Order Successor of all nodes in Binary Search Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Remove all leaf nodes from the binary search tree
- Find the node with maximum value in a Binary Search Tree
- Find the node with maximum value in a Binary Search Tree using recursion
- Find maximum among all right nodes in Binary Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Sum and Product of minimum and maximum element of Binary Search Tree
- Maximum height of the binary search tree created from the given array
- Count permutations of given array that generates the same Binary Search Tree (BST)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.