Same idea, but i think it will explore BST property later

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int max = 0;
public int[] findMode(TreeNode root) {
List<Integer> tmp = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();
inOrder(root, map);
// for (Map.Entry s : map.entrySet()) {
// if ((int)s.getValue() == max) {
// tmp.add((int)s.getKey());
// }
// }
for (int key : map.keySet()) {
if (map.get(key) == max) {
tmp.add(key);
}
}
int[] res = new int[tmp.size()];
int id = 0;
for (int num : tmp) {
res[id++] = num;
}
return res;
}
private void inOrder(TreeNode root, HashMap<Integer, Integer> map) {
if (root == null) {
return;
}
inOrder(root.left, map);
map.put(root.val, map.getOrDefault(root.val, 0) + 1);
int count = map.get(root.val);
max = Math.max(max, count);
inOrder(root.right, map);
}
}