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 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).
思路:
对于本题中的二叉搜索树,节点的排列是有顺序的,左节点<=当前节点<=右节点。也就是说,假设这样一种情况:存在大于等于3个的连续相同的节点,且当前节点存在左右子树,那么相同的三个节点一定是:当前节点、左子树中最大的节点和右子树中最小的节点。
这里我们容易想到的是中序遍历(对于整个树,先遍历左节点,再遍历中间节点,最后遍历右节点),使用中序遍历遍历二叉搜索树,可以获得一个从小到大的排好序的升序序列。
所以分析到这里,题目就转化成:给定一个升序序列,寻找重复次数最多的数字 , 所以
/**
* 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) {
tmp_cnt = ;
max_cnt = ;
cur_value = ;
inorder(root);
return result;
}
private:
int tmp_cnt;
int max_cnt;
int cur_value;
vector<int> result;
void inorder(TreeNode* root){
if (root == NULL){
return;
}
// 遍历左子树
inorder(root->left);
tmp_cnt++;
if (cur_value != root-> val){
cur_value = root-> val;
tmp_cnt = ;
}
if (tmp_cnt > max_cnt){
max_cnt = tmp_cnt;
result.clear();
result.push_back(root->val);
}else if (tmp_cnt == max_cnt){
result.push_back(root->val);
}
// 遍历右子树
inorder(root->right);
}
};
501. Find Mode in Binary Search Tree的更多相关文章
- 35. leetcode 501. Find Mode in Binary Search Tree
501. Find Mode in Binary Search Tree Given a binary search tree (BST) with duplicates, find all the ...
- 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 ...
- 501. Find Mode in Binary Search Tree【LeetCode by java】
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- 501. Find Mode in Binary Search Tree查找BST中的众数
[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数
这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...
- 【leetcode】501. Find Mode in Binary Search Tree
class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
随机推荐
- 网卡的 Ring Buffer 详解
1. 网卡处理数据包流程 网卡处理网络数据流程图: 图片来自参考链接1 上图中虚线步骤的解释: DMA 将 NIC 接收的数据包逐个写入 sk_buff ,一个数据包可能占用多个 sk_buff , ...
- 震惊!计算机连0.3+0.6都算不对?浅谈IEEE754浮点数算数标准
>>> 0.3+0.6 0.8999999999999999 >>> 1-0.9 0.09999999999999998 >>> 0.1+0.1+ ...
- Python基础(zip方法)
zip函数: 描述:将zip函数中的两个可迭代对象参数按对应索引值进行匹配组合,得到zip对象.(拉链式函数) zip函数简单应用如下: #-----------------zip函数-------- ...
- nodejs 使用 ethers创建以太坊钱包
创建钱包创建钱包流程: 生成随机助记词 => 通过助记词创建钱包=>钱包信息和加密明文(私钥和密码加密) 导入钱包通过插件提供方法,根据助记词|keyStore|私钥,找到钱包信息(地址和 ...
- HTTP 内容编码,也就这 2 点需要知道 | 实用 HTTP
Hi,大家好,我是承香墨影! HTTP 协议在网络知识中占据了重要的地位,HTTP 协议最基础的就是请求和响应的报文,而报文又是由报文头(Header)和实体组成.大多数 Http 协议的使用方式,都 ...
- 知识小罐头07(tomcat8请求源码分析 下)
感觉最近想偷懒了,哎,强迫自己也要写点东西,偷懒可是会上瘾的,嘿嘿!一有写博客的想法要赶紧行动起来,养成良好的习惯. ok,继续上一篇所说的一些东西,上一篇说到Connector包装了那两个对象,最后 ...
- java~springboot~ibatis Invalid bound statement (not found)原因
事实起因 最近在ORM上使用了ibatis,感觉挺繁琐的,没有jpa来的直接,但项目非要用也没有办法,最近在进行开发过程中出现了一个问题Invalid bound statement (not fou ...
- AR增强现实开发介绍(续)
AR增强现实开发介绍(续) ---开发基础篇 开发增强现实技术,无论是商业级应用,还是面向幼儿教育的游戏产品,都需要从了解.获取.下载增强现实插件开始.目前全世界使用量最大公认最好的增强现实插件是高通 ...
- Asp.Net Core 轻松学-一行代码搞定文件上传
前言 在 Web 应用程序开发过程中,总是无法避免涉及到文件上传,这次我们来聊一聊怎么去实现一个简单方便可复用文件上传功能:通过创建自定义绑定模型来实现文件上传. 1. 实现自定义绑定模型 1 ...
- C#中的CultureInfo类
CultureInfo类位于System.Globalization命名空间内,这个类和命名空间许多人都不是很熟悉,实际我们在写程序写都经常间接性的接触这个类,当进行数字,日期时间,字符串匹配时,都会 ...