501 - Find Mode in Binary Search Tree (BST mode)

meaning of the title

Given a BST, find the mode of val ue (there may be more than one)

follow up: Can you just use O(1) space? (Stack space is not included in binary tree traversal)

thinking

Algorithm 1

O(n) Time, O(n) Space

Traverse the binary tree and use a vis [] array to record how many times each number appears. In order to avoid vis [] being too big to explode memory, unordered_map can be used instead.

Algorithm 2

O(n) Time, O(1) Space

We know that an important property of BST is that its ordinal traversal result is an ascending array.

For an ordered array, we only need to traverse the statistics twice to find its modulus.

This problem only needs to first traverse the BST sequence to find out the number of times corresponding to the number of crowds, and then traverse the statistical results again.

Code

Algorithm 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    unordered_map<int, int> vis;
    vector<int> ans;
    int _max = 0;
public:
    void dfs(TreeNode* root) {
        if (!root) return;
        vis[root->val]++;
        if (vis[root->val] > _max) _max = vis[root->val];
        dfs(root->left);
        dfs(root->right);
    }

    vector<int> findMode(TreeNode* root) {
        _max = 0;
        dfs(root);
        for (auto it = vis.begin(); it != vis.end(); it++) {
            if (it->second == _max) ans.push_back(it->first);
        }
        return ans;
    }
};

Algorithm 2

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    int max_;
    int pre, count;
        bool flag;
    vector<int> ans;

    void reset() {
        flag = 0;
        count = 0;
    }

    void cal(int x, int sta) {
        if (!flag) {
            if (!sta) max_ = 1;
            pre = x;
            count++;
            if (sta && count == max_) ans.push_back(x);
            flag = true;
        } else {
            if (pre == x) {
                count++;
                if (!sta && count > max_) max_ = count;
                if (sta && count == max_) ans.push_back(x);
            } else {
                count = 1;
                pre = x;
                if (sta && count == max_) ans.push_back(x);
            }
        }
    }

    void inorder(TreeNode* root, int sta) {
        if (!root) return;
        inorder(root->left, sta);
        cal(root->val, sta);
        inorder(root->right, sta);
    }

public:
    Solution() : max_(0), pre(-1), count(0), flag(false) {}
    vector<int> findMode(TreeNode* root) {
        inorder(root, 0);
        reset();
        inorder(root, 1);
        return ans;
    }
};

Posted by dianaqt on Thu, 14 Feb 2019 10:24:18 -0800