Given the root of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.

If the tree has more than one mode, return them in any order.

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.


&nbsp;
Example 1:

Input: root = [1,null,2,2]
Output: [2]


Example 2:

Input: root = [0]
Output: [0]


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [1, 104].
	-105 &lt;= Node.val &lt;= 105


&nbsp;
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
class Solution(object):
    prev = None
    max_count = 0
    current_count = 0 
    result = []

    def findMode(self, root):
        self.dfs(root)
        return self.result

    def dfs(self, node):
        if not node: return
        self.dfs(node.left)
        self.current_count = 1 if node.val != self.prev else self.current_count + 1
        if self.current_count == self.max_count:
            self.result.append(node.val)
        elif self.current_count > self.max_count:
            self.result = [node.val]
            self.max_count = self.current_count
        self.prev = node.val
        self.dfs(node.right)
class Solution {
    public int[] findMode(TreeNode root) {
        if(root==null) return new int[0];
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(); //we are taking map to count each and every value of the tree and the no of times they occurs in the tree
        Queue<TreeNode> qu = new LinkedList<TreeNode>(); // to itereate over the tree
        List<Integer> list = new ArrayList<Integer>(); //to save our result into a dynamic arraylist then will convert into static array for return it
        qu.add(root);                     // add the first root node into queue to iterate over the tree
        while(!qu.isEmpty()) {   
            TreeNode tmp = qu.poll();               //we poll out the node which is last inputed to the queue
            if(map.containsKey(tmp.val)) {           //we are checking through the map wheather the value this node have already stored into the map or not
                map.put(tmp.val, map.get(tmp.val)+1);     //the value is already stored then we just increase the count by 1
            }
            else {
                map.put(tmp.val, 1);     //if the value is unique then we store it to the map with the count 1
            }
            if(tmp.left!=null) qu.add(tmp.left);    //this way we are checking wheather left node has any value or not respect to the current poped element of queue
            if(tmp.right!=null) qu.add(tmp.right);    //the same thing of the above just this is for right node of respective poped out node
        }
        int max = Integer.MIN_VALUE;   //we are taking it because of requirement to identify highest no of repeated node available in this tree 
        for(Integer key : map.keySet()) {  //we are using keySet() for iterating over the map here key is differernt nodes and value is the no of count they have in this tree
            if(map.get(key)>max) { //if anything we find have greater value then previous maximum no of node like 2 2 2 - value 3, 3 3 3 3 - value 4 so now 4 is the maximum now 
                list.clear(); //just to clear previous data that are stored into that list
                max = map.get(key);   //now max will replaced by the new no of count of a node
                list.add(key);    //we are adding the key which has most no of count
            }
            else if(max==map.get(key)) { //if we found another node which also has present maximum no of node count in the tree
                list.add(key);  //we are also adding those key
            }
        }
		//now we just create an array transfer hole data that arraylist has and then return
        int[] res = new int[list.size()];
        for(int i=0; i<list.size(); i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
class Solution {
public:

    void inorder(TreeNode* root, unordered_map<int,int>& mp){
        if(root==NULL) return;

        inorder(root->left, mp);
        mp[root->val]++;
        inorder(root->right, mp);
    }

    vector<int> findMode(TreeNode* root) {
        unordered_map<int,int> mp;
        int maxf=INT_MIN;
        vector<int> ans;

        inorder(root, mp);

        for(auto i=mp.begin(); i!=mp.end(); i++){
            maxf = max(i->second, maxf);
        }

        for(auto i=mp.begin(); i!=mp.end(); i++){
            if(i->second==maxf) ans.push_back(i->first);
        }

        return ans;

    }
};
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var findMode = function(root) {
	const dup = new Map();

	helper(root, dup);

	let res = [];
	let max = Number.MIN_VALUE;
	for(const [key, value] of dup) {
		if (value > 1 && value >= max) {
			res.push(key);
			max = Math.max(max, value);
		}
	}

	return res.length > 0 ? res : [...dup.keys()];
};

function helper(root, dup) {
	if(root !== null) {
		dup.set(root.val, ~~dup.get(root.val) + 1);
		helper(root.left, dup);
		helper(root.right, dup);
	}
}
Find Mode in Binary Search Tree