Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

501. Find Mode in Binary Search Tree 🌟

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:

Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count). 🌟🌟

Hash map with DFS

Code


class Solution {
private:
    void dfs(TreeNode* root, unordered_map<int, int>& mp, int& max) {
        if (root == nullptr) {
            return;
        }
        mp[root->val]++;
        if (mp[root->val] > max) {
            max = mp[root->val];
        }
        dfs(root->left, mp, max);
        dfs(root->right, mp, max);
    }
public:
    vector<int> findMode(TreeNode* root) {
        unordered_map<int, int> mp;
        vector<int> res;
        int max = 0;
        dfs(root, mp, max);
        for(auto& [k, v] : mp) {
            if (v == max) {
                res.push_back(k);
            }
        }
        return res;
    }
};

Iterative DFS

Code

class Solution {
public:
    vector<int> findMode(TreeNode* root)
    {
        unordered_map<int, int> mp;
        // iterative dfs
        stack<TreeNode*> st;
        st.push(root);

        while (!st.empty()) {
            TreeNode* node = st.top();
            st.pop();
            mp[node->val]++;
            if (node->left) st.push(node->left);
            if (node->right) st.push(node->right);
        }

        vector<int> res;
        int mx = 0;

        for (auto& [k, v] : mp) {
            mx = max(mx, v);
        }

        for (auto& [k, v] : mp) {
            if (v == mx) res.push_back(k);
        }

        return res;
    }
};

Without extra space(without Hashmap)

Code


class Solution {
    void inorder(TreeNode* root, int& prev, int& currFreq, int& maxFreq, vector<int>& res)
    {
        if (!root) return;
        inorder(root->left, prev, currFreq, maxFreq, res);
        if (prev == root->val) {
            currFreq++;
        } else {
            currFreq = 1;
        }
        if (currFreq == maxFreq) {
            res.push_back(root->val);
        } else if (currFreq > maxFreq) {
            res.clear();
            res.push_back(root->val);
            maxFreq = currFreq;
        }
        prev = root->val;
        inorder(root->right, prev, currFreq, maxFreq, res);
    }

public:
    vector<int> findMode(TreeNode* root)
    {
        vector<int> res;
        int maxFreq = 0, currFreq = 0, prev = INT_MIN;
        inorder(root, prev, currFreq, maxFreq, res);
        return res;
    }
};

Morris Traversal

Coming Soon …

TODO: solve using morris traversal.