Lc501_二叉搜索树中的众数
package com.example.leetcode;
import java.util.*;
/**
* @description: 501. 二叉搜索树中的众数
* 给定一个有相同值的二叉搜索树(BST),找出 BST 中的所有众数(出现频率最高的元素)。
* <p>
* 假定 BST 有如下定义:
* <p>
* 结点左子树中所含结点的值小于等于当前结点的值
* 结点右子树中所含结点的值大于等于当前结点的值
* 左子树和右子树都是二叉搜索树
* 例如:
* 给定 BST [1,null,2,2],
* <p>
* 1
* \
* 2
* /
* 2
* 返回[2].
* <p>
* 提示:如果众数超过1个,不需考虑输出顺序
* <p>
* 进阶:你可以不使用额外的空间吗?(假设由递归产生的隐式调用栈的开销不被计算在内)
* @author: licm
* @create: 2021-04-21 15:40
**/
public class Lc501_二叉搜索树中的众数 {
/**
* 思路 : 普通的递归遍历 map记录结果集,最后根据结果集进行筛选
* @param root
* @return
*/
public static int[] findMode(TreeNode root) {
Map<Integer, Integer> map = new HashMap<>();
travesal(root, map);
List<Integer> temps = new ArrayList<>();
int max = 0;
for (Map.Entry<Integer, Integer> m : map.entrySet()) {
max = max>=m.getValue()?max:m.getValue();
}
for (Map.Entry<Integer, Integer> m : map.entrySet()) {
String key = m.getKey().toString();
if (Integer.valueOf(m.getValue().toString()) == max) {
temps.add(m.getKey());
}
}
int[] res = new int[temps.size()];
for (int i = 0; i < temps.size(); i++) {
res[i] = temps.get(i);
}
return res;
}
static void travesal(TreeNode root, Map<Integer, Integer> res) {
if (root == null) {
return;
}
if (res.containsKey(root.val)) {
res.put(root.val, res.get(root.val) + 1);
} else {
res.put(root.val, 1);
}
travesal(root.left, res);
travesal(root.right, res);
}
public static void main(String[] args) {
Integer[] arr = new Integer[]{0,1};
TreeNode root = CreateNode.createTree(arr).get(0);
int[] res = findMode(root);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
}
不恋尘世浮华,不写红尘纷扰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2020-04-22 关于2020疫情春招面试总结(俩年经验,面24,收获20offer)
2020-04-22 关于渗透的练习网站