问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 dic
where r.Value == max
select 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. [Swift]LeetCode501. 二叉搜索树中的众数 | Find Mode in Binary Search Tree

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

  2. Java实现 LeetCode 501 二叉搜索树中的众数

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

  3. Leetcode:1305. 两棵二叉搜索树中的所有元素

    Leetcode:1305. 两棵二叉搜索树中的所有元素 Leetcode:1305. 两棵二叉搜索树中的所有元素 思路 BST树中序历遍有序. 利用双指针法可以在\(O(n)\)的复杂度内完成排序. ...

  4. [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...

  5. Leetcode501.Find Mode in Binary Search Tree二叉搜索树中的众数

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

  6. 【LeetCode 235_二叉搜索树】Lowest Common Ancestor of a Binary Search Tree

    解法一:递归 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if (root == NULL | ...

  7. LeetCode501.二叉搜索树中的众数

    题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...

  8. 刷题-力扣-230. 二叉搜索树中第K小的元素

    230. 二叉搜索树中第K小的元素 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kth-smallest-element-in-a ...

  9. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  10. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

随机推荐

  1. SpringMVC集成Mybatis

    1.pom.xml中添加引入架包 <dependency> <groupId>mysql</groupId> <artifactId>mysql-con ...

  2. swfupload控件文件上传大小限制设置

    swfupload控件,是我在开发过程中用到的上传文件的控件,非常实用和方便.但最近碰到一些问题,解决之后进行一下整理. 因为用户上传文件的大小限制增加,导致原本上传控件时,文件的大小需要进行调整和限 ...

  3. odoo12数据库自动化备份

    数据库自动备份模块地址 https://github.com/Yenthe666/auto_backup#8.0 目前支持8以上的版本 odoo12的配置步骤 1.下载模块到自己的模块目录 2.登录o ...

  4. 数据库(十二):pymysql

    进击のpython ***** 数据库--pymysql 数据库就算是学习完毕了,但是我们学习数据库的本质是什么? 是想让数据库像文件存储一样将信息存储起来供我们调用 那回归本行,我就应该是用pyth ...

  5. .NET Core 微服务—API网关(Ocelot) 教程 [二]

    上篇文章(.NET Core 微服务—API网关(Ocelot) 教程 [一])介绍了Ocelot 的相关介绍. 接下来就一起来看如何使用,让它运行起来. 环境准备 为了验证Ocelot 网关效果,我 ...

  6. R 数据读取与写入

    路径 getwd() #获取当前工作路径 setwd() #设置工作路径 获取普通文本数据 x = read.table("data.txt") #通过路径直接获取 x = rea ...

  7. 计划工程师dadafksjh

    Markdown常规语法 标题 # 代表一级标题 ## 代表二级标题 -- ####### 代表六级标题 一级标题 二级标题 三级标题 六级标题 列表 有序列表 1. 数字1 + . + 空格 无序列 ...

  8. 分布式锁-Redis方案

    #!/usr/bin/env python # coding=utf-8 import time import redis class RedisLock(object): def __init__( ...

  9. Linux重定向用法详解

    大家好,我是良许. 相信大家平时都会有需要复制粘贴数据的时候,如果是打开文件进行复制粘贴,就不可避免的需要较多的鼠标与键盘的操作,就会比较繁琐.那么有没有可以省掉这些繁琐操作的复制粘贴的方法呢? 答案 ...

  10. It还是高薪行业不?—软件测试

    It还是高薪行业不?—软件测试 谁都希望拿高薪,但是并不是所有人.所有地方都能的:甚者培训出来还不能就业的大有人在,也不是所有人都适合培训后就业(年龄.学历.专业.期望就业地点.不同行业转行还是有很大 ...