[抄题]:

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

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 要处理的情况写if, else if,不处理的不用管
  2. inorder遍历本质是dfs,也有退出条件

[二刷]:

  1. maxcount最大值需要保留,不能清空

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

curcount > maxcount时,更改众数,重置modecount = 1

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  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中的众数的更多相关文章

  1. [LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

    这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回 ...

  2. 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  ...

  3. 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 ...

  4. 501. Find Mode in Binary Search Tree

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  5. 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 ...

  6. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  8. [LeetCode] 99. Recover Binary Search Tree(复原BST) ☆☆☆☆☆

    Recover Binary Search Tree leetcode java https://leetcode.com/problems/recover-binary-search-tree/di ...

  9. 99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

随机推荐

  1. PS基础教程[7]如何为照片瘦身

    有没有对自己的身材有所抱怨,有没有想过让自己的照片便得苗条一些,其实只有你想不到的,没有我们做不到的,PS中可以很简单的将我们的身体进行美化瘦身,本次经验我们就来学习一下简单的操作. 方法 1.打卡P ...

  2. SharedPreference作用及数据操作模式

    SharedPreference是Android平台上的一个轻量级的存储类,用来保存应用的一些常用配制,比如Activity状态,Activtiy暂停,将此Activity的状态保存到SharedPr ...

  3. ①HttpURLConnection通过报文提交

    在进行短信发送的接口,因厂家不同,有的厂家会采用报文的格式进行短信请求的发送与接收.本文主要介绍利用HttpURLConnection进行短信报文的请求与响应. 一般的url请求分为两种,一种是GET ...

  4. 十大Java人物

    James Gosling : Java之父文/陶文 作 为Java之父,James Gosling的名字可谓是耳熟能详.当人们评论一种编程语言时,总喜欢捎带着把下蛋的母鸡一起带上.Java做为中国的 ...

  5. [转]HTTP 协议中的 Transfer-Encoding

    本文作为我的博客「HTTP 相关」专题新的一篇,主要讨论 HTTP 协议中的 Transfer-Encoding.这个专题我会根据自己的理解,以尽量通俗的讲述,结合代码示例和实际场景来说明问题,欢迎大 ...

  6. 洛谷 1099 ( bzoj 1999 ) [Noip2007]Core树网的核

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1999 <算法竞赛进阶指南>346页.https://www.cnblogs.co ...

  7. python学习之logging

    学习地址:http://blog.csdn.net/zyz511919766/article/details/25136485 首先如果我们想简要的打印出日志,可以: import logging l ...

  8. Linux Samba目录服务搭建与Java客户端访问

    前言: 本文比较简略,只求快速入门,若要了解详情,推荐一篇文章:http://www.cnblogs.com/mchina/archive/2012/12/18/2816717.html 1,安装sa ...

  9. Java开发进阶技能(附文章引用链接)

    一.玩转源码 1.Java+Selenium3方法篇0-如何在Eclipse上查看Selenium源码 (在github上下载源码)

  10. 单片机keil C中的data、bdata、idata、xdata、hdata、pdata、code解释

    从数据存储类型来说,8051系列有片内.片外程序存储器,片内.片外数据存储器,片内程序存储器还分直接寻址区和间接寻址类型,分别对应code.data.xdata.idata以及根据51系列特点而设定的 ...