一:题目
二:上码
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public void Order(TreeNode root,HashMap<Integer,Integer>map) {if (root == null) return ;Order(root.left,map);map.put(root.val,map.getOrDefault(root.val,0)+1);Order(root.right,map);}public int[] findMode(TreeNode root) {HashMap<Integer,Integer>map = new HashMap<>();Order(root,map);int maxx = Integer.MIN_VALUE;int count = 0;int count1 = 0;for (Integer key:map.keySet()) { if (map.get(key) > maxx) {maxx = map.get(key);}} for (Integer key:map.keySet()) { if (map.get(key) == maxx) {count++;}}int[] ans = new int[count];for (Integer key:map.keySet()) { if (map.get(key) == maxx) {ans[count1++] = key;}}return ans;}
}