赞
踩
- /**
- * 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 {
- ArrayList<Integer> reslist =new ArrayList<>();
- int count;
- int maxcount;
- TreeNode pre;
- public int[] findMode(TreeNode root) {
- sub_iter(root);
- int[] res = new int[reslist.size()];
- for(int i=0;i<reslist.size();i++){
- res[i] = reslist.get(i);
- }
- return res;
- }
- public void sub_iter(TreeNode node){
- if(node==null){
- return;
- }
- sub_iter(node.left);
- if(pre==null){
- count=1;
- }else if(pre.val == node.val){
- count++;
- }else{
- count=1;
- }
- pre = node;
- if(count==maxcount) reslist.add(node.val);
- if(count>maxcount){
- maxcount = count;
- reslist.clear();
- reslist.add(node.val);
- }
- sub_iter(node.right);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。