Given the root
of a binary search tree (BST) with duplicates, return all the mode(s) (i.e., the most frequently occurred element) in it.
If the tree has more than one mode, return them in any order.
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.
Example 1:

Input: root = [1,null,2,2] Output: [2]
Example 2:
Input: root = [0] Output: [0]
Constraints:
- The number of nodes in the tree is in the range
[1, 104]
. -105 <= Node.val <= 105
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).A:
------------inorder BST traversal, save all values in the vector<int> and then count the mode ----------------递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> findMode(TreeNode* root) {
unordered_map<int,int> M;
helper(root, M);
// get max mode count
int maxCount = 0;
for(auto p : M){
maxCount = max(maxCount, p.second);
}
vector<int> res;
for(auto p : M){
if(p.second == maxCount){
res.push_back(p.first);
}
}
return res;
}
private:
void helper(TreeNode* root, unordered_map<int,int> &M){
if(!root)
return;
M[root->val] += 1;
helper(root->left, M);
helper(root->right, M);
}
};
-------------- O(0) extra space. --------------- 是真的可以不用声明任何变量的
---------(哇哈哈,别人分享的都没有做出来,大爷我做出来了. 哇哈哈哈---------------
错误发生在 判断res[1] == 0 时, 一开始写成了 res.empty()
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> findMode(TreeNode* root) {
// for NO any extra space, thus MIGHT increase time complexity
return getModeWithCount(root, findModCount(root));
}
private:
int findModCount(TreeNode* root){ // get the cound of any mode
// get count of node , rooted at root node
if(!root)
return 0;
return max( getValueCount(root, root->val),
max(findModCount(root->left), findModCount(root->right))
);
}
int getValueCount(TreeNode* root, int val){
if(!root)
return 0;
return (root->val == val? 1:0) + getValueCount(root->left, val) + getValueCount(root->right, val);
}
vector<int> getModeWithCount(TreeNode * root, int modeCount){
if(!root){
return vector<int>{};
}
return mergeVectors(getModeWithCount(root->left, modeCount),
getModeWithCount(root->right, modeCount),
(getValueCount(root, root->val) == modeCount)? (vector<int>{root->val}):(vector<int>{})
);
}
vector<int> mergeVectors(vector<int> a, vector<int> b, vector<int> c){
a.insert(a.end(), b.begin(), b.end());
a.insert(a.end(), c.begin(), c.end());
return a;
}
};
错误发生在 判断res[1] == 0 时, 一开始写成了 res.empty()
No comments:
Post a Comment