美文网首页
501. Find Mode in Binary Search

501. Find Mode in Binary Search

作者: 程序猪小羊 | 来源:发表于2018-06-11 08:22 被阅读9次

题目
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than or equal to the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

找众数

用use HashMap<Integer, frequency>

(List??)
返回int[]
traverse: inorder, pre, post - anyone is fine

O(n)时间复杂度

R:
Java:Map与HashMap,Hashtable,HashSet比较

  • List in Java

有序、可重复; 可以持续扩展大小(array不可以)
ListList集合代表一个元素有序、可重复的集合,集合中每个元素都有其对应的顺序索引。注意: List集合默认按元素的添加顺序设置元素索引,例如第 ...

==> 用List的原因是:List可以持续扩展大小,而且数组不可以。

Java数组声明后其长度就固定了,不能增加长度。 要创建一个可扩展的数组可以使用ArrayList或Vector。

class Solution {
    HashMap<Integer, Integer> map;
        // = new HashMap<>();
    int max = 0; 
    
    public int[] findMode(TreeNode root) {
        if(root == null) {return new int[0];}
        this.map = new HashMap<>();
        
        inorder(root);
        
        List<Integer> list = new LinkedList<>();
        for(int key: map.keySet()) {
            if(map.get(key) == max) {list.add(key);}
        }
        
        int[] res = new int[list.size()];
        for(int i = 0; i<res.length; i++) res[i] = list.get(i);
        // transfer the LinkedList list to int[] res
        
        return res;
    }
    
    private void inorder(TreeNode node) {
        if(node.left != null) {inorder(node.left);}
        map.put(node.val, map.getOrDefault(node.val, 0) + 1);
        max = Math.max(max, map.get(node.val));
        // constantly update the maximum node.val
        if(node.right != null) {inorder(node.right);}
    }
}

评价:
O(n) Time, O(n) Space;
it doesn't use the property of BST.

// P⊆NP⊆PSPACE=NPSPACE⊆EXPTIME

O(1) Space

public class Solution {
    
    public int[] findMode(TreeNode root) {
        inorder(root);
        modes = new int[modeCount];
        modeCount = 0;
        currCount = 0;
        inorder(root);
        return modes;
    }

    private int currVal;
    private int currCount = 0;
    private int maxCount = 0;
    private int modeCount = 0;
    
    private int[] modes;

    private void handleValue(int val) {
        if (val != currVal) {
            currVal = val;
            currCount = 0;
        }
        currCount++;
        if (currCount > maxCount) {
            maxCount = currCount;
            modeCount = 1; // BSTinorder遍历时,单调递增的属性
        } else if (currCount == maxCount) {
            if (modes != null)
                modes[modeCount] = currVal;
            modeCount++;
        }
    }
    
    private void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        handleValue(root.val);
        inorder(root.right);
    }
}

遍历了两遍,但是只用了一个modes[],只占用O(1)Space。
第一遍找到众数with highest frequency,并且记为N;
第二遍将所有出现了N次的数组都存在一个modes里。

I think the way to do it properly is to do two passes.

每次遍历一个数字Val时:
currVal = Val, 记录当前数字出现的次数currCount;

对得到对currCount进行判断:
是否出现次数大于maxCount,是则 - 1. 更新最大频率maxCount,2. 新的maxCount 对应的数字个数 更新为1(后期出现频率相同的数字时modeCount++)。

相关文章

网友评论

      本文标题:501. Find Mode in Binary Search

      本文链接:https://www.haomeiwen.com/subject/tbqqeftx.html