题目:
题解:
class Solution {int base, count, maxCount;List<Integer> answer = new ArrayList<Integer>();public int[] findMode(TreeNode root) {TreeNode cur = root, pre = null;while (cur != null) {if (cur.left == null) {update(cur.val);cur = cur.right;continue;}pre = cur.left;while (pre.right != null && pre.right != cur) {pre = pre.right;}if (pre.right == null) {pre.right = cur;cur = cur.left;} else {pre.right = null;update(cur.val);cur = cur.right;}}int[] mode = new int[answer.size()];for (int i = 0; i < answer.size(); ++i) {mode[i] = answer.get(i);}return mode;}public void update(int x) {if (x == base) {++count;} else {count = 1;base = x;}if (count == maxCount) {answer.add(base);}if (count > maxCount) {maxCount = count;answer.clear();answer.add(base);}}
}