98. Validate Binary Search Tree
1.中序
首先明确BST的定义,左子树的所有点都要小于根节点,不是单指其中一组
这里用中序,所以一定是从小到大的顺序
class Solution {
public:
long maxVal = LONG_MIN;
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
bool left = isValidBST(root->left);
if(root->val > maxVal) maxVal = root->val;
else return false;
bool right = isValidBST(root->right);
return left&&right;
}
};
注意:题目中给的范围超过了int,所以用了long
2.使用辅助函数,增加函数参数列表,在参数中携带额外信息,将这种约束传递给子树的所有节点
(看懂了,但是迭代的部分思路不是特别清晰)
class Solution {
public:
bool checkBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode){
if(root == NULL) return true;
if(minNode != NULL && root->val <= minNode->val) return false;
if(maxNode != NULL && root->val >= maxNode->val) return false;
return checkBST(root->left, minNode, root) && checkBST(root->right, root, maxNode);
}
bool isValidBST(TreeNode* root) {
return checkBST(root, NULL, NULL);
}
};
注意相等的情况
530. Minimum Absolute Difference in BST
class Solution {
public:
int minDif = INT_MAX;
TreeNode* pre = NULL;
void traversal(TreeNode* curr){
if(curr == NULL) return;
traversal(curr->left);
if(pre != NULL){
minDif = min(minDif, curr->val-pre->val);
}
pre = curr;
traversal(curr->right);
}
int getMinimumDifference(TreeNode* root) {
traversal(root);
return minDif;
}
};
注意:
1.合理利用BST的特征
2.在树中如何利用双指针法
501. Find Mode in Binary Search Tree
1.应用BST特征(中序)
class Solution {
private:
int maxCount = 0;
int count = 0;
TreeNode* pre = NULL;
vector<int> res;
void traversal(TreeNode* curr){
if(curr == NULL) return;
traversal(curr->left);
if(pre == NULL){
count = 1;
}else if(pre->val == curr->val){
count ++;
}else{
count = 1;
}
pre = curr;
if(count == maxCount){
res.push_back(curr->val);
}
if(count > maxCount){
res.clear();
res.push_back(curr->val);
maxCount = count;
}
traversal(curr->right);
return;
}
public:
vector<int> findMode(TreeNode* root) {
traversal(root);
return res;
}
};
2.可应用在不是BST的树的时候
1)用map存储,这里要注意map是不能对value进行排序的,所以要把map转变成vector, 在用sort排序
2)有关sort:
sort(first_pointer,first_pointer+n,cmp)
该函数可以给数组,或者链表list、向量排序
可以不写cmp(函数排序方式), 默认为升序
class Solution {
private:
void traversal(TreeNode* root, unordered_map<int, int>& maxVal_Count){
if(root == NULL) return;
traversal(root->left, maxVal_Count);
maxVal_Count[root->val]++;
traversal(root->right, maxVal_Count);
return;
}
bool static cmp(const pair<int, int>& a, const pair<int, int>& b){
return a.second > b.second;
}
public:
vector<int> findMode(TreeNode* root) {
vector<int> res;
unordered_map<int, int> map;
if(root == NULL) return res;
traversal(root, map);
vector<pair<int, int>> vec(map.begin(), map.end());
sort(vec.begin(),vec.end(), cmp);
res.push_back(vec[0].first);
for(int i=1; i<vec.size(); i++){
if(vec[i].second == vec[0].second) res.push_back(vec[i].first);
else break;
}
return res;
}
};
236. Lowest Common Ancestor of a Binary Tree
(突然看不明白了😩,明天再说吧)
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL) return NULL;
if(root == p || root == q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left != NULL && right != NULL) return root;
else if(left == NULL && right != NULL) return right;
else if(left != NULL && right == NULL) return left;
else return NULL;
}
};
文章介绍了几个与二叉搜索树(BST)相关的问题,包括验证BST的正确性、找到BST中的最小差值以及查找BST中的众数。解决方案涉及中序遍历、递归和迭代方法,并强调了BST的特性在解决问题时的重要性。
338

被折叠的 条评论
为什么被折叠?



