501. Find Mode in Binary Search Tree查找BST中的众数
[抄题]:
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].
[暴力解法]:
时间分析:
空间分析:hashmap:n
[优化后]:
时间分析:
空间分析:各种count
[奇葩输出条件]:
返回具体元素,不是次数。所以反过来 nums[次数] = 元素。
[奇葩corner case]:
[思维问题]:
[一句话思路]:
curcount > maxcount时,重置modecount = 1
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 要处理的情况写if, else if,不处理的不用管
- inorder遍历本质是dfs,也有退出条件
[二刷]:
- maxcount最大值需要保留,不能清空
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
curcount > maxcount时,更改众数,重置modecount = 1
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
- 全局变量用于声明类型 一次就行 eg int,有返回值的单个函数中赋值
[关键模板化代码]:
if (curCount > maxCount) {
maxCount = curCount;
modeCount = 1;
}else if (curCount == maxCount) {
if (modes != null) //after first inorder
modes[modeCount] = curValue;
modeCount ++;
}
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
//ini
private int curValue;
private int[] modes;
private int curCount = 0;
private int maxCount = 0;
private int modeCount = 0; public int[] findMode(TreeNode root) {
//inorder twice
inorder(root);
modes = new int [modeCount];
curCount = 0;
//maxCount = 0;need jilu
modeCount = 0;//re start
inorder(root);
return modes;
} public void handleValue(int val) {
if (val != curValue) {
curValue = val;
curCount = 0;
}
curCount++; if (curCount > maxCount) {
maxCount = curCount;
modeCount = 1;
}else if (curCount == maxCount) {
if (modes != null) //after first inorder
modes[modeCount] = curValue;
modeCount ++;
}
} public void inorder(TreeNode root) {
if (root == null) return ;
inorder(root.left);
handleValue(root.val);
inorder(root.right);
}
}
501. Find Mode in Binary Search Tree查找BST中的众数的更多相关文章
- [LeetCode]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
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 ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆
Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...
- 99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- MarkDown小技巧
MarkDown编辑 MarkDown编辑的时候,可以直接插入HTML的编辑手法. 例如如果想要将一段话中的某一个或者某几个字设为其他颜色,但又不想将这几个字与这段话分割的时候,可以这样 你可以这样将 ...
- linux【基础命令】
最近在学linux,避免一些命令忘记,所以在此记录一下: linux文件列表遍历 ls -a 列出所有的文件及文件夹 包括隐藏的ls -l 列出文件目录的详细信息 history 查看历史命令ctrl ...
- 网络爬虫必备知识之urllib库
就库的范围,个人认为网络爬虫必备库知识包括urllib.requests.re.BeautifulSoup.concurrent.futures,接下来将结合爬虫示例分别对urllib库的使用方法进行 ...
- 重温CLR(八 ) 泛型
熟悉面向对象编程的开发人员都深谙面向对象的好处,其中一个好处是代码重用,它极大提高了开发效率.也就是说,可以派生出一个类,让他继承基类的所有能力.派生类只需要重写虚方法,或添加一些新方法,就可定制派生 ...
- phpredis的使用
phpredis的具体使用方法可以参照:https://github.com/phpredis/phpredis
- Visual Studio Code教程:基础使用和自定义设置
一.界面介绍 1.1 界面介绍 1.2 文件夹和文件的打开 文件——>打开文件夹/打开文件 1.3 新建文件/文件夹 新建文件: a. 文件——>新建文件: b. 按Ctrl+n; c. ...
- 洛谷 1600 (NOIp2016) 天天爱跑步——树上差分
题目:https://www.luogu.org/problemnew/show/P1600 看TJ:https://blog.csdn.net/clove_unique/article/detail ...
- textView 添加超链接(两种实现方式)
在textView添加超链接,有两种方式,第一种通过HTML格式化你的网址,一种是设置autolink,让系统自动识别超链接,下面为大家介绍下这两种方法的实现 在textView添加超链接,有两种方式 ...
- jmeter ---单个server最大连接数的设置
为了模拟浏览器关于建立多少并行的链接设置,在jmeter中也有相关的设置 在HTTP请求设置页面,勾选“Use concurrent pool" 选型,并将pool size设置为所需的并发 ...
- Android 控件之Spinner
Spinner用来显示列表项,类似于一组单选框RadioButton.下面瞥一下它的效果. 源码下载 一.概述 Spinner是一个每次只能选择所有项的一个项的控件.它的项来自于与之相关联的适配器中. ...