问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4086 访问。

给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。

假定 BST 有如下定义:

结点左子树中所含结点的值小于等于当前结点的值
结点右子树中所含结点的值大于等于当前结点的值
左子树和右子树都是二叉搜索树

给定 BST [1,null,2,2],

1
    \
     2
    /
   2
返回[2].

提示:如果众数超过1个,不需考虑输出顺序

进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)


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.

Given BST [1,null,2,2],

1
    \
     2
    /
   2
return [2].

Note: If a tree has more than one mode, you can return them in any order.

Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4086 访问。

public class Program {public static void Main(string[] args) {var root = new TreeNode(1) {left = new TreeNode(3) {left = new TreeNode(5),right = new TreeNode(7)},right = new TreeNode(9)};var res = FindMode(root);ShowArray(res);Console.ReadKey();}static void ShowArray(int[] array) {foreach(var num in array) {Console.Write($"{num} ");}Console.WriteLine();}public static int[] FindMode(TreeNode root) {if(root == null) return new int[0];var dic = new Dictionary<int, int>();PreOrder(root, ref dic);var max = dic.Values.Max();return (from r in dicwhere r.Value == maxselect r.Key).ToArray();}public static void PreOrder(TreeNode root, ref Dictionary<int, int> dic) {if(root == null) return;if(dic.ContainsKey(root.val)) {dic[root.val]++;} else {dic[root.val] = 1;}PreOrder(root?.left, ref dic);PreOrder(root?.right, ref dic);}public class TreeNode {public int val;public TreeNode left;public TreeNode right;public TreeNode(int x) { val = x; }}}

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4086 访问。

1 3 5 7 9

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#501-二叉搜索树中的众数​​​​​​​(Find Mode in Binary Search Tree)相关推荐

  1. [力扣] 501. 二叉搜索树中的众数

    501 二叉搜索树中的众数 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 例如: 给定 BST [1,null,2,2], 返回[2]. 提示:如果众数超 ...

  2. Suzy找到实习了吗 Day 21 | 二叉树进行中:530. 二叉搜索树的最小绝对差,501. 二叉搜索树中的众数,236. 二叉树的最近公共祖先

    530. 二叉搜索树的最小绝对差 题目 给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 .差值是一个正数,其数值等于两值之差的绝对值. solution # Defi ...

  3. LeetCode 501. 二叉搜索树中的众数(中序遍历)

    文章目录 1. 题目 2. 中序遍历 1. 题目 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等 ...

  4. LeetCode 501. 二叉搜索树中的众数

    https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/ 难度:简单   给定一个有相同值的二叉搜索树(BST),找出 BST ...

  5. 74. Leetcode 501. 二叉搜索树中的众数 (二叉搜索树-中序遍历类)

    给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素).如果树中有不止一个众数,可以按 任意顺序 返回.假定 BST 满足如下定义:结 ...

  6. leetcode 501. 二叉搜索树中的众数(Java版)

    题目 https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/ 题解 中序遍历二叉搜索树,可以得到一个有序序列. 遍历这个有序 ...

  7. LeetCode 501二叉搜索树中的众数-简单

    给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假定 BST 有如下定义: 结点左子树中所含结点的值小于等于当前结点的值 结点右子树中所含结点的值大于等于当 ...

  8. leetcode 501. 二叉搜索树中的众数 思考分析

    目录 题目 1.不考虑BTS性质,直接寻找众数集合(利用map) 2.考虑BTS的中序遍历结果性质 题目 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素). 假 ...

  9. leetcode系列-501. 二叉搜索树中的众数

    题目描述:给你一个含重复值的二叉搜索树(BST)的根节点 root ,找出并返回 BST 中的所有 众数(即,出现频率最高的元素).如果树中有不止一个众数,可以按 任意顺序 返回.假定 BST 满足如 ...

  10. 力扣501. 二叉搜索树中的众数(JavaScript)

    //使用双指针记录同一值的出现次数 var findMode = function(root) {let p=rootlet count=0 //次数let max=1let arr=[]const ...

最新文章

  1. 1024 | 不加班,带你们一起薅当当的羊毛!
  2. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
  3. 理解图像的傅里叶变换
  4. 深度学习【目标检测】
  5. 34muduo_net库源码分析(十)
  6. MacOS使用top命令查看进程使用内存
  7. 创建数据账号只有个别表的权限_只有普通权限账号,如何把远程数据库中该用户的数据表导入到本地数据库?...
  8. document.execCommand
  9. 课设——C语言学生成绩管理系统
  10. 《疯狂的程序员》绝影新闻
  11. 远程桌面同步本地计算机,微软更新远程桌面应用现在终于可以在本地和远程计算机上复制文件...
  12. 电视盒子ADB常用命令
  13. lidar_camera_calib代码解读-优化部分
  14. 家中买的计算机配置,配置,教您买电脑主要看哪些配置
  15. UiPath Excel 数据筛选修改
  16. 对偶性(duality)以及KKT条件
  17. SpringBoot GA/PRE/SNAPSHOT含义
  18. [数据压缩作业1]利用Audacity分析浊音、清音、爆破音|RGB文件三通道分量的熵计算
  19. 粒子群算法在物流中心选址中的应用
  20. AMS1117的输入电压范围

热门文章

  1. Python深入-Python的内存管理
  2. LeetCode 717. 1-bit and 2-bit Characters
  3. Shell——文件包含
  4. 深度学习图像预处理——分类
  5. ubuntu software updater已意外关闭 解决办法
  6. xCode 安装Mobile Device Framework出错的问题的解决方法
  7. django-模板变量
  8. Django组件-用户认证
  9. 区块链100讲:不做码农做矿工,该怎么和爹妈解释
  10. iOS音频播放 (二):AudioSession 转