501. Find Mode in Binary Search Tree*

501. Find Mode in Binary Search Tree*

https://leetcode.com/problems/find-mode-in-binary-search-tree/

题目描述

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.

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.

For example:

Given BST [1,null,2,2],

   1
    \
     2
    /
   2
 

return [2].

Note: If a tree has more than one mode, you can return them in any order.

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

C++ 实现 1

如果可以用哈希表, 统计每个元素出现的个数, 那么此题相对会简单一些; C++ 实现 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:
    unordered_map<int, int> record;
    void preOrder(TreeNode *root) {
        if (!root)
            return;
        record[root->val] ++;
        preOrder(root->left);
        preOrder(root->right);
    }
public:
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        if (!root)
            return res;
        preOrder(root);
        int imax = INT32_MIN;
        for (auto &iter : record) {
            if (iter.second > imax) {
                res = vector<int>{iter.first};
                imax = iter.second;
            }
            else if (iter.second == imax)
                res.push_back(iter.first);
        }
        return res;
    }
};

C++ 实现 2

需要了解到, 对于 BST, 如果对其进行中序遍历, 那么得到的结果是有序的, 比如:

1 1 2 2 2 3 3

对于这样一个有序的序列, 如何找到出现次数最多的元素? 我们需要两个指针, 其中 prev 指向相同元素的第一个, 而 root 指向当前元素. count 记录当前元素的个数, 如果它的值大于 max_count, 就需要更新出现次数最多的元素. 注意 count 初始化为 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 {
public:
    vector<int> findMode(TreeNode* root) {
        inorder(root);
        return res;
    }
private:
    void inorder(TreeNode *root) {
        if (!root) return;
        inorder(root->left);
        if (prev) {
            if (root->val == prev->val) count ++;
            else count = 1;
        }
        if (count == maxCount) {
            res.push_back(root->val);
        }
        else if (count > maxCount) {
            res.clear();
            res.push_back(root->val);
            maxCount = count;
        }
        prev = root;
        inorder(root->right);
    }
private:
    TreeNode *prev;
    vector<int> res;
    int count = 1;
    int maxCount = 0;
};
发布了352 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Eric_1993/article/details/104549497