leetcode501二叉搜索树中的众数刷题打卡

简介: leetcode501二叉搜索树中的众数刷题打卡
501. 二叉搜索树中的众数

题目描述

给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素)。

如果树中有不止一个众数,可以按 任意顺序 返回。

假定 BST 满足如下定义:

  • 结点左子树中所含节点的值 小于等于 当前节点的值
  • 结点右子树中所含节点的值 大于等于 当前节点的值
  • 左子树和右子树都是二叉搜索树

题解思路

  • 迭代法
  • 中序遍历二叉树,将节点放入一个vector数组中,三个for循环来处理该vector
  • 第一个for循环将vector放进map中
map<int, int> m;
for(int i = 0; i < v.size(); i++){
    m[v[i]]++;
}
  • 第二个for循环找到频率最大的key
int max = INT_MIN;
for(map<int, int>::iterator it = m.begin(); it != m.end(); it++){
   max = max < it->second ? it->second : max; 
}
  • 第三个for循环根据最大频率值找到与之匹配的key并且放入结果vector
vector<int> result;
for(map<int, int>::iterator it = m.begin(); it != m.end(); it++)
    if(it->second == max) result.push_back(it->first);
  • 完整代码
class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        stack<TreeNode*> s;
        vector<int> v;
        int diff = 1000000;
        s.push(root);
        while(!s.empty()){
            TreeNode* cur = s.top(); s.pop();
            if(cur){
                if(cur->right) s.push(cur->right);
                s.push(cur);
                s.push(nullptr);
                if(cur->left) s.push(cur->left);
            }else{
                cur = s.top(); s.pop();
                v.push_back(cur->val);
            }
        }
        map<int, int> m;
        for(int i = 0; i < v.size(); i++){
            m[v[i]]++;
        }
        int max = INT_MIN;
        for(map<int, int>::iterator it = m.begin(); it != m.end(); it++){
            max = max < it->second ? it->second : max; 
        } 
        vector<int> result;
        for(map<int, int>::iterator it = m.begin(); it != m.end(); it++)
            if(it->second == max) result.push_back(it->first);
        return result;     
    }
};
  • 迭代
  • 中序遍历大框架来进行操作
void inorder(TreeNode* root){
  if(!root) return;
    inorder(root->left);
    // 处理逻辑
    inorder(root->right);
}
  • 由于二叉搜索树的中序遍历是升序的,所以可以用记录前一个节点的方法来处理众数,设置一个窗口变量,某数的频率有多高这个窗口变量就有多大,然后设置一个最大窗口变量来代表结果,下列是其具体处理逻辑
if(postnode){
    if(postnode->val == root->val)
        chuangkou++;
    else
        chuangkou = 1;
}else{
    chuangkou = 1;
}
postnode = root;
if(chuangkou == maxchuangkou){
    result.push_back(root->val);
}
if(chuangkou > maxchuangkou){
    maxchuangkou = chuangkou;
    result.clear();
    result.push_back(root->val);
}
  • 完整代码
class Solution {
public:
    int chuangkou;
    int maxchuangkou;
    vector<int> result;
    TreeNode *postnode;
    void inorder(TreeNode* root){
        if(!root) return;
        inorder(root->left);
        if(postnode){
            if(postnode->val == root->val)
                chuangkou++;
            else
                chuangkou = 1;
        }else{
            chuangkou = 1;
        }
        postnode = root;
        if(chuangkou == maxchuangkou){
            result.push_back(root->val);
        }
        if(chuangkou > maxchuangkou){
            maxchuangkou = chuangkou;
            result.clear();
            result.push_back(root->val);
        }
        inorder(root->right); 
    }
    vector<int> findMode(TreeNode* root) {
          inorder(root);
          return result;
    }
};


相关文章
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
27 6
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
28 4
|
13天前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
42 2
|
4天前
|
算法
LeetCode第96题不同的二叉搜索树
文章介绍了LeetCode第96题"不同的二叉搜索树"的解法,利用动态规划的思想和递推公式,通过计算以任意节点为根的不同二叉搜索树的数量,解决了该问题。
LeetCode第96题不同的二叉搜索树
|
13天前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
16 7
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
13 4
|
14天前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
32 5
|
13天前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
25 3
|
13天前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
11 3
|
13天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
25 3