/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int count = 1;
private Integer pre = null;
private int max = 0;
public int[] findMode(TreeNode root) {
if (root == null) return new int[0];
List<Integer> res = new ArrayList<Integer>();
inOrder(root, res);
if (count > max) {
res.clear();
res.add(pre);
} else if (count == max) {
res.add(pre);
}
int[] result = new int[res.size()];
int i = 0;
for (Integer e : res) result[i++] = e;
return result;
}
public void inOrder(TreeNode root, List<Integer> res) {
if (root == null) return;
inOrder(root.left, res);
if (pre != null) {
if (pre == root.val) {
count++;
} else {
if (count > max) {
max = count;
res.clear();
res.add(pre);
} else if (count == max) {
res.add(pre);
}
count = 1;
}
}
pre = root.val;
inOrder(root.right, res);
}
}