代码随想录算法训练营Day21 | 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先
LeetCode 530.二叉搜索树的最小绝对差
思路:
中序遍历
1.数组法 2.指针法 3.迭代法
//数组法
class Solution {
public:
vector<int> vec;
int minDiff = INT_MAX;
void traversal(TreeNode* root){
if (!root) return;
traversal(root->left);
vec.push_back(root->val);
traversal(root->right);
}
int getMinimumDifference(TreeNode* root) {
vec.clear()
traversal(root);
if (vec.size() < 2) return 0;
for(int i=1; i<vec.size(); i++){
if(vec[i]-vec[i-1] < minDiff) minDiff = vec[i]-vec[i-1];
}
return minDiff;
}
};
//指针法
class Solution {
public:
TreeNode* pre = NULL;
int minDiff = INT_MAX;
void traversal(TreeNode* node){
if(!node) return;
traversal(node->left);
if(pre && node->val-pre->val < minDiff) minDiff = node->val-pre->val;
pre = node;
traversal(node->right);
}
int getMinimumDifference(TreeNode* root){
traversal(root);
return minDiff;
}
};
//迭代法
class Solution {
public:
int minDiff = INT_MAX;
int getMinimumDifference(TreeNode* root){
if(!root) return 0;
stack<TreeNode*> st;
TreeNode* pre = NULL;
TreeNode* cur = root;
while(cur || !st.empty()){
if(cur){
st.push(cur);
cur = cur->left;
}
else{
cur = st.top();
st.pop();
if(pre) minDiff = min(minDiff, cur->val - pre->val);
pre = cur;
cur = cur->right;
}
}
return minDiff;
}
};
注意 :
- 迭代法,先左,后右
LeetCode 501.二叉搜索树中的众数
思路:
//迭代法
class Solution {
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
stack<TreeNode*> st;
TreeNode* cur = root;
TreeNode* pre = NULL;
int maxCount = INT_MIN;
int count = 0;
while(cur || !st.empty()){
if(cur){
st.push(cur);
cur = cur->left;
}
else{
cur = st.top();
st.pop();
if(!pre) count = 1;
else if(pre->val == cur->val) count++;
else count = 1;
if(count == maxCount) res.push_back(cur->val);
if(count > maxCount){
res.clear();
maxCount = count;
res.push_back(cur->val);
}
pre = cur;
cur = cur->right;
}
}
return res;
}
};
//遍历法
class Solution {
public:
vector<int> res;
int count = 0;
TreeNode* pre = NULL;
int maxCount = INT_MIN;
void traversal(TreeNode* node){
if(!node) return;
traversal(node->left);
if(!pre) count = 1;
else if(node->val==pre->val) {
count ++;
}
else count = 1;
if(count == maxCount) res.push_back(node->val);
if(count>maxCount){
maxCount = count;
res.clear();
res.push_back(node->val);
}
pre = node;
traversal(node->right);
}
vector<int> findMode(TreeNode* root) {
res.clear();
traversal(root);
return res;
}
};
//数组法
class Solution {
public:
//unordered_map<int, int> map;
bool static cmp(pair<int, int>& lhs, pair<int, int>& rhs){
return lhs.second > rhs.second;
}
void traversal(TreeNode* node, unordered_map<int, int>& map){
if(!node) return;
map[node->val] ++;
traversal(node->left, map);
traversal(node->right, map);
}
vector<int> findMode(TreeNode* root) {
unordered_map<int, int> map;
traversal(root, map);
vector<pair<int,int>> vec(map.begin(),map.end());
vector<int> res;
sort(vec.begin(),vec.end(), cmp);
for(pair<int,int> it:vec){
if(it.second == vec[0].second) res.push_back(it.first);
}
return res;
}
};
注意 :
- 数组法为通用方法
LeetCode 236. 二叉树的最近公共祖先
思路:
分别查找左、右是否存在p和q,哪边有,则返回哪边。 注意特殊情况,如果root=p 或者 root=q,则返回root。
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return root;
if(root==p || root==q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left && !right) return left;
else if(!left && right) return right;
else if (left&&right) return root;
return NULL;
}
};
注意 :
- if(rootp || rootq) return root; 此条件不可省