代码拉取完成,页面将自动刷新
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。