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 ...
随机推荐
- linux中磁盘分区以及如何将磁盘挂载到系统中
fdisk 分区 fdisk /dev/sdb -p查看当前分区-t 改分区号-w将分区表写进磁盘-n创建分区 -p主分区 -e拓展分区 partprobe 让内核更新分区信息cat /proc/pa ...
- 21天学通C++_Day3_Part1
今天奔跑吧兄弟来杭电了,决定不去看,宅在科协继续啃(反正去了也看不到咯),继续继续,今天白天没课,希望可以更两个. 0.C风格字符串 在数组中间把某个字符替换成‘\0’并不会改变数组的长度,但是显示时 ...
- Windows 7 中的 God Mode
Windows 7系统中隐藏了一个秘密的“God Mode”,字面上译为“上帝模式”,但似乎叫它“万能模式”更贴切一些.God Mode其实就是一个简单的文件夹窗口,但包含了几乎所有Windows 7 ...
- C# 反射之SqlDatareader转换为Model实体.
虽说反射的效率比较低,但是在当今的时代,盛行的大数据,以及高并发的产生,硬件的产能正在逐渐的提升,所以我们可以用空间来换取时间.反射所消耗的那些性能问题其实在企业级开发而言也就无所谓了.二 : 反射得 ...
- 洛谷【P1757】通天之分组背包
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:https://www.luogu.org/problemnew/show/P ...
- dubbox下载编译运行demo
最近公司要搞微服务改造,拿了一个小项目开刀,找来找去,还是偏向当当的dubbox作为分布式服务框架.这里介绍下怎么一条龙跑起一个demo. 1.下载代码 因为代码放在github上,所以我们直接用Ec ...
- 阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql
此教程中使用的相关IP等设置,在你的环境中要做相应修改. 使用之前更新apt-get,因为服务器基本上是一个裸系统 apt-get update;apt-get upgrade; 1 我们使用root ...
- java排序。。。
题目:n位学生,m位评委,去掉一个最高分,和一个最低分,选手最后得分 package com.aini; import java.util.Arrays; public class WDS { int ...
- 第15届浙江省赛 E LIS
LIS Time Limit: 1 Second Memory Limit: 65536 KB Special Judge DreamGrid is learning the LI ...
- PHP中静态变量的使用
1.定义静态变量 public static $endpoint,$accessKeyId,$accessKeySecret,$bucket; 2.静态变量赋值 protected function ...