106、【树与二叉树】leetcode ——501. 二叉搜索树中的众数:双指针法+哈希表法(C++版本)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
原题链接501. 二叉搜索树中的众数

解题思路

一、递归法

1双指针方法

设置一个指针pre指向前一个遍历结点另一个指针root指向当前遍历结点。设置一个变量count记录当前等于该结点数值的结点个数。maxCount记录众数。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* pre = NULL;
    int count = 1, maxCount = 1;
    vector<int> res;
    vector<int> findMode(TreeNode* root) {
        if(!root)       return res;
        // 左
        findMode(root->left);

        // 中
        // 当pre为空或pre与root指向元素的值不相等则让当前count=1。若二者指向相等则count++
        if(!pre || pre->val != root->val)       count = 1;
        else if(pre->val == root->val)          count++;
        // 更新pre指针
        pre = root;
        // 当找到的这个元素个数与之前添加过的最大元素个数相同则添加。若更大清空原来存储结果加入新结果
        if(count == maxCount)                   res.push_back(root->val);
        else if(count > maxCount) {
            maxCount = count;
            res.clear();
            res.push_back(root->val);
        }

        // 右
        findMode(root->right);
        return res;
    }
};

2哈希表法

设置一个向量path按中序遍历将结点存入其中。设置一个哈希表record统计各值出现的次数用maxCount获取最大次数。最后将与出现最大次数相同的数加入到结果集中。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:    
    // 用path按中序遍历顺序记录结点
    vector<int> path;    
    void traversal(TreeNode* root) {
        if(!root)           return ;
        traversal(root->left);
        path.push_back(root->val);
        traversal(root->right);
    }
    vector<int> findMode(TreeNode* root) {        
        vector<int> res;
        traversal(root);
        // 记录各值出现频率
        unordered_map<int, int> record;
        for(int num : path) {
            record[num]++;
        }
        // 获取最大次数值
        int maxCount = 1;
        for(auto it = record.begin(); it != record.end(); it++) {
            if(it->second > maxCount)       maxCount = it->second;
        }
        // 将众数添加大结果集
        for(auto it = record.begin(); it != record.end(); it++) {
            if(it->second == maxCount)      res.push_back(it->first);
        }

        return res;
    }
};

二、迭代法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        int count = 1, maxCount = 1;
        stack<TreeNode*> st;
        TreeNode* cur = root;
        TreeNode* pre = NULL;
        vector<int> res;
        while(cur || !st.empty()) {
            while(cur) {
                st.push(cur);
                cur = cur->left;
            }
            cur = st.top();     st.pop();
            // 更新count
            if(!pre || pre->val != cur->val)
                count = 1;
            else if(pre->val == cur->val)
                count++;
            // 根据count和maxCount比较选择是否加入结果集
            if(count == maxCount)
                res.push_back(cur->val);
            else if(maxCount < count) {
                res.clear();
                res.push_back(cur->val);
                maxCount = count;
            }
            pre = cur;
            cur = cur->right;
        }
        return res;
    }
};

参考文章501.二叉搜索树中的众数

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: c++