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]:

[思维问题]:

新建数组指定空间,多大不知道,所以需要提前遍历一下

[一句话思路]:

先存maxcount,符合要求之后再存modecount

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

[画图]:

[一刷]:

  1. 有返回值的单个函数中,必须把全局变量再写一遍
  2. 要处理的情况写if, else if,不处理的不用管
  3. inorder遍历本质是dfs,也有退出条件

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

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

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

原文地址:https://www.cnblogs.com/immiao0319/p/8973041.html

时间: 2024-08-27 02:58:45

501. Find Mode in Binary Search Tree查找BST中的众数的相关文章

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

这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义. 不过如果是保存每层递归内的信息,就需要传参数. 本题显然是需要全局参数 /** * Definition for a binary tree node. * public class TreeNode { * int

LeetCode 501. Find Mode in Binary Search Tree(寻找二叉查找树中出现次数最多的值)

题意:寻找二叉查找树中出现次数最多的值 /** * 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: int ma = 0; int cnt = 1; TreeNode

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 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 nod

[Leetcode] Binary tree -- 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 nod

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 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 nod

[LeetCode] 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 nod

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

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

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? /** * Definition for a binary

[leetcode]98. Validate Binary Search Tree验证BST

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys