问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. JavaScript动画实例:旋转的正三角形

    给定一个正三角形的重心坐标为(x0,y0),高为h,可以用如下的语句绘制一个底边水平的正三角形. ctx.beginPath(); ctx.moveTo(x0,y0-h*2/3); ctx.lineT ...

  2. Oracle DataGuard主库丢失归档日志后备库的RMAN增量恢复一例

    第一部分  问题描述和环境状态确认 ----1. 问题场景 Oracle DataGuard主库丢失archivelog,如何不重建备库完成同步? 在Oracle DataGuard主从同步过程中可能 ...

  3. 软件测试必备技能,带你学习jmeter!

    一:jmeter用户变量设置: 1.在线程组鼠标右击--添加--配置元件-用户定义的变量, 2.根据业务需求自定义变量的名称,添加需要的变量和对应的值 3.在脚本对应位置添加参数 二:文件参数化: 两 ...

  4. C#程序员装机必备软件及软件地址

    装机必备 Visio2010 下载 http://gd.ddooo.com/visio2010_12530.rar Office 安装包 http://xz.cncrk.com:8080/soft/k ...

  5. 第二章 Java基础知识(下)

    2.1.分支结构(if.switch) 2.1.1.if语句 格式一: if (关系表达式) { 语句体; } 流程一: ①首先计算关系表达式的值 ②如果关系表达式的值为true就执行语句体 ③如果关 ...

  6. Tkinter常用简单操作

        截图来自北京尚学堂 手册:http://effbot.org/tkinterbook/ 2020-04-20

  7. PHP gmstrftime() 函数

    ------------恢复内容开始------------ 实例 根据区域设置格式化 GMT/UTC 日期和时间: <?phpecho(gmstrftime("%B %d %Y, % ...

  8. PHP getName() 函数

    实例 返回 XML 元素及其子元素的名称: <?php$xml=<<<XML高佣联盟 www.cgewang.com<?xml version="1.0&quo ...

  9. Skill 返回list中的倒数第二个数据

    https://www.cnblogs.com/yeungchie/ code procedure(ycLast2(list) prog((index ouput) unless(type(list) ...

  10. 4.11 省选模拟赛 序列 二分 线段树优化dp set优化dp 缩点

    容易想到二分. 看到第一个条件容易想到缩点. 第二个条件自然是分段 然后让总和最小 容易想到dp. 缩点为先:我是采用了取了一个前缀最小值数组 二分+并查集缩点 当然也是可以直接采用 其他的奇奇怪怪的 ...