[抄题]:

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. libQt5Core.so: undefined reference to `dlclose@GLIBC_2.4'

    /******************************************************************************** * libQt5Core.so: u ...

  2. Java Web 高性能开发,前端的高性能

    Java Web 高性能开发,第 2 部分: 前端的高性能 Web 发展的速度让许多人叹为观止,层出不穷的组件.技术,只需要合理的组合.恰当的设置,就可以让 Web 程序性能不断飞跃.Web 的思想是 ...

  3. C# 操作嵌入的资源

    被嵌入的程序执行文件 namespace AppTest { class Program { static void Main(string[] args) { Console.WriteLine(& ...

  4. #507. 「LibreOJ NOI Round #1」接竹竿 dp

    题目: 题解: 我们考虑把每对花色相同的牌看作区间. 那么如果我们设 \(f_i\) 表示决策在 \([1,i]\) 内的最优答案. 那么有 \(f_i = max\{max\{(f_{j-1}+\s ...

  5. 脚本工具---自动解析mysql建表语句,生成sqlalchemy表对象声明

    常规建表语句: CREATE TABLE `test_table` ( `id` int(11) NOT NULL, `name` char(64) NOT NULL, `password` char ...

  6. SQL Server数据库优化经验总结

    优化数据库的注意事项: 1.关键字段建立索引. 2.使用存储过程,它使SQL变得更加灵活和高效. 3.备份数据库和清除垃圾数据. 4.SQL语句语法的优化.(可以用Sybase的SQL Expert, ...

  7. (转)Oracle执行字符串

    declare v_out ); begin execute immediate 'select p_guid from c_itcomp where rownum = 1 ' into v_out; ...

  8. java继承实例

    题目:1./*定义一个Person类,这个类的属性有:name.age.color类有构造方法给3个属性赋值类有run方法,能计算出十年后的年龄并输出.类有eat方法,能改变自己的name和color ...

  9. kali下apche配置多网站

    设置文件地址为 :/etc/apache2/sites-available/default 添加: <VirtualHost *:80>       ServerName www.php. ...

  10. mysql索引设计

    mysql索引设计 1.B树与B+树的区别?B-Tree:一个节点可以拥有大于2个子节点的平衡多叉树,所有关键字在整颗树中出现,包括在非叶子节点也能命中, 叶子节点之间没有链表B+Tree:每个叶子节 ...