[LeetCode] 501. Find Mode in Binary Search Tree

电商秒杀场景下,如何用Redis+Lua脚本实现原子性库存扣减(附完整代码) 本文针对电商秒杀场景下的高并发与超卖难题,详细解析了如何利用Redis的原子性命令与Lua脚本实现可靠的库存扣减。通过对比多种方案,阐述了Redis+Lua作为黄金搭档的技术原理,并提供了从基础扣减到预扣减模型的完整代码实现,确保在高并发下数据的一致性与系统性能。 阅读详情

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],
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).

解析

找到二叉树中的众数。

解法1:inorder(递归)

递归中序遍历二叉树,建立一个哈希表,保存一个最大个数。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        int mx = 0;
        unordered_map<int, int> m; 
        inorder(root, m, mx);
        for (auto a : m) {
            if (a.second == mx) {
                res.push_back(a.first);
            }
        }
        return res;
    }
    
    void inorder(TreeNode* root, unordered_map<int, int>& m, int& mx){
        if(!root) return;
        inorder(root->left, m, mx);
        m[root->val] ++;
        mx = max(mx, m[root->val]);
        inorder(root->right, m, mx);
    }
};

解法2:inorder(迭代)

迭代中序遍历二叉树,建立一个哈希表,保存一个最大个数。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(!root) return {};
        vector<int> res;
        int mx = 0;
        unordered_map<int, int> m; 
        stack<TreeNode*> s;
        TreeNode* p = root;
        while(!s.empty() || p){
            while(p){
                s.push(p);
                p = p->left;
            }
            p = s.top();
            s.pop();
            m[p->val] ++;
            mx = max(mx, m[p->val]);
            p = p->right;
        }
        for (auto a : m) {
            if (a.second == mx) {
                res.push_back(a.first);
            }
        }
        return res;
    }
};

解法3:inorder(递归不使用额外空间)

利用二叉树的中序遍历有序的特性,对二叉树递归中序遍历,设置一个pre节点,一个count记录当前节点次数,当pre不为空时,不是第一个节点,与前一个节点值比较,如果相等则count+1,否则count=1;
当count大于最大次数mx时,清空res,加入当前节点,并将mx=count;pre更新为当前节点p。

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        int mx = 0;
        int count = 1;
        TreeNode* pre = NULL;
        inorder(root, pre, count, mx, res);
        return res;
    }
    
    void inorder(TreeNode* p, TreeNode*& pre, int& count,int& mx, vector<int>& res){
        if(!p) return;
        inorder(p->left, pre, count,mx,res);
        if(pre){
            count = (pre->val == p->val) ? count+1:1;
        }
        if(count>=mx){
            if(count>mx) res.clear();
            res.push_back(p->val);
            mx = count;
        }
        pre = p;
        inorder(p->right, pre, count,mx,res);
    }
};

解法4:inorder(迭代不使用额外空间)

迭代中序遍历,类似于解法3

class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        if(!root) return {};
        vector<int> res;
        int mx = 0;
        int count =1;
        stack<TreeNode*> s;
        TreeNode* p = root;
        TreeNode* pre = NULL;
        while(!s.empty() || p){
            while(p){
                s.push(p);
                p = p->left;
            }
            p = s.top();s.pop();
            if(pre){
                count = (pre->val == p->val) ? count+1:1;
            }
            if(count>=mx){
                if(count>mx) res.clear();
                res.push_back(p->val);
                mx = count;
            }
            pre = p;
            p = p->right;
        }
        return res;
    }
};

参考

http://www.cnblogs.com/grandyang/p/6436150.html

比利时风电光伏负荷一体化数据集解析与应用 本文深度解析了由比利时输电系统运营商Elia集团提供的风电光伏负荷一体化数据集。该数据集整合了负荷、风电与光伏出力数据,并包含多时间尺度的官方预测值,时间跨度长达四年,是研究能源转型、构建电力预测模型及分析电网运行的宝贵资源。文章详细介绍了数据价值、字段含义、实战应用场景(如预测模型基准测试、风光互补性分析)以及数据处理指南,为相关领域的研究与开发提供了高质量数据基础和实践参考。 阅读详情

相关推荐

深入探索 SEED-VII:情感脑电图研究的新利器

SEED-VII是上海交通大学最新发布的多模态情感脑电图数据集,在传统EEG数据基础上增加了眼动、生理信号等多维信息,支持更精细的情感识别研究。该数据集提供了丰富的情感维度标注(效价和唤醒度),可用于算法基准测试、跨模态情感融合及个性化模型开发等研究场景。在应用层面,SEED-VII可推动人机交互、心理健康监测、教育娱乐等领域的情感计算技术发展。文章还提供了PyTorch加载该数据集的代码示例,为研究者使用该数据集提供了技术参考。

qq_32516809的博客 1745

[leetcode]501. Find Mode in Binary Search Tree

[leetcode]501. Find Mode in Binary Search Tree Analysis 讨厌的人总是那么讨厌!—— [ummmm~] Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) i...

weixin_32135877的博客 200

python基础·练习1(字符串作业)

python基础·练习1字符串作业一、填空二、编程初级中级 字符串作业 一、填空 1.已知s2 = ‘123’,则:s2 * 3 = ___________; s2 + str(456) = ___________; s2 = '123' print(s2 * 3) #123123123 print(s2 + str(456)) #123456 2.已知s1 = ‘123456789’。则: s1[:3] = ________; s1[-5:] =____________; s1[::2]=______;

qq_47326711的博客 8122

LeetCode 501. Find Mode in Binary Search Tree

给定具有重复项的二叉搜索树 (BST) 的根,返回其中的所有模式(即最常出现的元素)。

m0_54644067的博客 516

YOLOv8剪枝实战:基于torch_pruning的模型优化与性能提升指南

本文详细介绍了如何使用torch_pruning工具对YOLOv8模型进行结构化剪枝,以实现模型轻量化与性能提升。通过实战步骤解析,包括环境搭建、依赖图分析、迭代剪枝与微调策略,帮助开发者有效减少模型参数量和计算量,提升在边缘设备上的推理速度,同时保持检测(detect)与分割(segment)任务的精度。

water的专栏 1122

国内外主流嵌入式GUI汇总

随着MCU资源增多,处理能力也强了,同时市场需求也增多了GUI功能也越来越丰富了,很多的嵌入式图形库都可以做出绚丽的界面。

m0_69914734的博客 3290

leetcode-501-Find Mode in Binary Search Tree

问题题目:[leetcode-501]思路常规题。遍历一遍,找出出现次数最大的值。 然后枚举哈希表即可。代码/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int

Kang_TJU的博客 1098

【MODIS处理工具】MRT(MODIS Reprojection Tool) 下载及安装

【MODIS处理工具】MRT(MODIS Reprojection Tool) 下载及安装

WW、forever的博客 2405

LeetCode //C - 501. Find Mode in Binary Search Tree

【代码】LeetCode //C - 501. Find Mode in Binary Search Tree。

Made in Code 832

C#串口通信从入门到精通(7)——整数/浮点数与二进制、八进制、十六进制之间的转换与C#实现

本文详细介绍由博主开发的DataConvert串口通信帮助类的用法,该帮助类涵盖了串口通信领域所有的数据转换方法,帮助大家快速编写串口通信程序!

qq_34059233的博客 2879

【YoloV11改进策略】基于流形约束超连接(mHC)增强YoloV11全局特征提取能力|独家复现|梁文峰署名论文

论文标题:流形约束超连接 (Manifold-Constrained Hyper-Connections, mHC)核心问题:针对超连接 (Hyper-Connections, HC) 扩展残差流宽度时破坏恒等映射、引发训练不稳定性和高内存开销的问题,提出一种约束性框架。目标:恢复残差连接的稳定性与可扩展性,同时保持HC的性能增益。基准模型:基于DeepSeek-V3的MoE架构,进行了3B、9B、27B模型的实验验证。论文图示mHC 是一个通用的HC扩展框架。

m0_47867638的博客 1349

leetcode 501. 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 c

淡然坊 459

SAP S4 HANA:按单生产(MTO)场景中的COGS分割

本文介绍了SAP S/4HANA中提供的各种选项,以在销售订单/商品是按订单生产(以下称为MTO)生产流程中的成本对象时,在基于帐户的COPA环境中报告COGS分割信息。

adam915的博客 1191

【Leetcode】501. Find Mode in Binary Search Tree 解题报告

在一棵二叉排序树(非严格定义)中查找出现最多的数字(s) 思路 对于二叉树的遍历有三种方式,先序后序中序,而这道题一定要用中序遍历,因为只有中序遍历才能将当前节点的值既算在左子树中又算在右子树中,不会因为访问了左子树而对右子树造成影响。 方法1 使用字典作为辅助空间(不满足要求 ) class Solution1: def findMode(self, root): &quot;&quot;...

很吵请安静 509

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

LeetCode|501. Find Mode in Binary Search Tree

时隔若干年后,我又开始刷leetcode了,看看这次会刷多少。

iLOVEJohnny的博客 633

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Leetcode——501. Find Mode in Binary Search Tree

#https://leetcode.com/problems/find-mode-in-binary-search-tree/?tab=Solutions Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given

好记性不如烂笔头的专栏 570

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[LeetCode 501] Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all themode(s)(the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contain...

ExcitedZhang的博客 131

leetcode 501 Find Mode in Binary Search Tree

第一次使用ruby来解决据结构相关的题目。

天天烤代码 964

Leetcode 501 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 c

夜色之浓,莫过于黎明前的黑暗。 265

LeetCode 501 Find Mode in Binary Search Tree

LeetCode 501 Find Mode in Binary Search Tree Problem Description: 返回二叉搜索树中出现最多次数的节点值的序列。 具体的题目信息: https://leetcode.com/problems/find-mode-in-binary-search-tree/description/ Solution: /** * De...

萤火虫啊飞呀飞 198

leetcode501. Find Mode in Binary Search Tree

501. Find Mode in Binary Search TreeGiven 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

rxt2012kc的博客 557

LeetCode501. Find Mode in Binary Search Tree

Given a binary search tree (BST) with duplicates, find all themode(s)(the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contain...

李歇特冯-兹拜因巴哈的博客 178

[leetcode] 501. Find Mode in Binary Search Tree

Description 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 n...

Learning from the mistakes 175

【leetcode】501. 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 contain...

ghscarecrow的博客 131
上一篇: [LeetCode] 450. Delete Node in a BST
下一篇: [LeetCode] 508. Most Frequent Subtree Sum
Peng_maple
博客等级 码龄10年 0粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值