package com.example.leetcode;
import java.util.*;
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]);
}
}
}