題目
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.
找眾數(shù)
用use HashMap<Integer, frequency>
(List??)
返回int[]
traverse: inorder, pre, post - anyone is fine
O(n)時間復(fù)雜度
R:
Java:Map與HashMap套腹,Hashtable电禀,HashSet比較
- List in Java
有序笤休、可重復(fù); 可以持續(xù)擴展大小(array不可以)
ListList集合代表一個元素有序葫松、可重復(fù)的集合腋么,集合中每個元素都有其對應(yīng)的順序索引珊擂。注意: List集合默認(rèn)按元素的添加順序設(shè)置元素索引费变,例如第 ...
==> 用List的原因是:List可以持續(xù)擴展大小,而且數(shù)組不可以挚歧。
Java數(shù)組聲明后其長度就固定了滑负,不能增加長度用含。 要創(chuàng)建一個可擴展的數(shù)組可以使用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遍歷時缸夹,單調(diào)遞增的屬性
} 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。
第一遍找到眾數(shù)with highest frequency佣蓉,并且記為N勇凭;
第二遍將所有出現(xiàn)了N次的數(shù)組都存在一個modes里。
I think the way to do it properly is to do two passes.
- One to find the highest number of occurrences of any value, and then a
- second pass to collect all values occurring that often.
https://leetcode.com/problems/find-mode-in-binary-search-tree/discuss/98101/Proper-O(1)-space
每次遍歷一個數(shù)字Val時:
currVal = Val, 記錄當(dāng)前數(shù)字出現(xiàn)的次數(shù)currCount寓盗;
對得到對currCount進行判斷:
是否出現(xiàn)次數(shù)大于maxCount傀蚌,是則 - 1. 更新最大頻率maxCount蘸吓,2. 新的maxCount 對應(yīng)的數(shù)字個數(shù) 更新為1(后期出現(xiàn)頻率相同的數(shù)字時modeCount++)。