1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LC_501.java 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
Neo_1001 提交于 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.*;
import java.util.function.IntFunction;
public class LC_501 {
public static void main(String[] args) {
}
public int[] findMode(TreeNode root) {
if (root == null) return null;
ArrayList<Integer> res = new ArrayList<>();
if (root.left == null & root.right == null) {
res.add(root.val);
Integer[] res_1 = res.toArray(new Integer[res.size()]);
int[] res_2 = new int[res.size()];
for (int i = 0; i < res_1.length; i++) {
res_2[i] = res_1[i].intValue();
}
return res_2;
}
HashMap<Integer, Integer> map = new HashMap<>();
traversal(root, map);
int most = 0;
Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
for (Map.Entry<Integer, Integer> entry : entries) {
most = Math.max(entry.getValue(), most);
}
for (Map.Entry<Integer, Integer> entry : entries)
if (entry.getValue() == most) res.add(entry.getKey());
Integer[] res_1 = res.toArray(new Integer[res.size()]);
int[] res_2 = new int[res.size()];
for (int i = 0; i < res_1.length; i++) {
res_2[i] = res_1[i].intValue();
}
return res_2;
}
public static void traversal(TreeNode root, HashMap<Integer, Integer> map) {
if (root == null) return;
traversal(root.left, map);
if (!map.containsKey(root.val)) {
map.put(root.val, 1);
} else {
int cont = map.get(root.val);
cont++;
map.put(root.val, cont);
}
traversal(root.right, map);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

搜索帮助