501. Find Mode in Binary Search Tree

 

题意为在一个BST当中寻找出现次数最多的数,并可能会有多个。

这道题中的BST有以下特征:

1)父节点的左子树所有节点的值都小于或者等于父节点的值;

2)父节点的右子树所有节点的值都大于或者等于父节点的值。

 

一开始自己的思路为,首先对树进行中序遍历,同时建立一个数组,以BST上的值类型作为数组的下标,数组的长度为BST上值的最大值,用于统计各个值类型出现的次数。最后直接返回值数组中值最大的下标。

代码如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int[] findMode(TreeNode root) {
12         List<Integer> arr = new ArrayList<Integer>();
13         List<Integer> res = new ArrayList<Integer>();
14         
15         helper(root, arr);
16         
17         int[] count = new int[arr.get(arr.size()-1) + 1];
18         
19         for( int i = 0 ; i < arr.size() ; i++){
20             count[arr.get(i)] ++;
21         }
22         
23         int maxCount = 0;
24         for( int i = 0 ; i < arr.get(arr.size()-1) + 1 ; i++){
25             maxCount = Math.max(count[i], maxCount);
26         }
27         
28         
29         for( int i = 0 ; i < arr.get(arr.size()-1) + 1 ; i++){
30             if( count[i] == maxCount ){
31                 res.add(i);
32             }
33         }
34         
35         int[] d = new int[res.size()];  
36         for(int i = 0;i<res.size();i++){  
37             d[i] = res.get(i);  
38         }
39         
40         return d;
41     }
42     
43     private void helper(TreeNode root, List<Integer> arr){
44         if(root == null) return;
45         helper(root.left, arr);
46         arr.add(root.val);
47         helper(root.right, arr);
48     }
49 }

 

报错为:

 

后面修改了思路为:先求出有多少个modes,最后再填充modes数组。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int[] modes;
    int modesnum = 0;
    int modescount = 0;
    int curcount = 0;
    int curvalue = 0;
    int index = 0;
    
    public int[] findMode(TreeNode root) {
        helper(root);
        modes = new int[modesnum];
        curcount = 0;
        curvalue = 0;
        helper(root);
        return modes;
    }
    
    private void helper(TreeNode root){
        if(root == null) return;
        helper(root.left);
        
        if(modes == null){
            if(curvalue != root.val){
                curvalue = root.val;
                curcount = 1;
                if(curcount > modescount){
                    modescount = curcount;
                    modesnum = 1;
                }else if(curcount == modescount){
                    modesnum ++;
                }
            }else if(curvalue == root.val){
                curcount++;
                if(curcount > modescount){
                    modescount = curcount;
                    modesnum = 1;
                }else if(curcount == modescount){
                    modesnum ++;
                }
            }
        }else{
            if(curvalue != root.val){
                curvalue = root.val;
                curcount = 1;
                if(curcount == modescount){
                    modes[index] = root.val;
                    index++;
                }
            }else if(curvalue == root.val){
                curcount ++;
                if(curcount == modescount){
                    modes[index] = root.val;
                    index++;
                }
            }
        }
        
        helper(root.right);
    }
}

 

 

END

 

posted @ 2018-04-10 14:29  sysu_kww  阅读(144)  评论(0编辑  收藏  举报