Day18 搜索二叉树进阶 | LeetCode 530. 二叉搜索树的最小绝对差, 501. 二叉搜索树中的众数, 236. 二叉树的最近公共祖先

文章介绍了在二叉搜索树中使用中序遍历和双指针求解最小绝对差问题,以及寻找众数和最近公共祖先的两种方法,涉及unordered_map和递归策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode 530. 二叉搜索树的最小绝对差

中序遍历,双指针
class Solution {
public:
    TreeNode* pre = nullptr;
    int result = INT_MAX;
    int getMinimumDifference(TreeNode* root) {
        traversal(root);
        return result;
    }

    void traversal(TreeNode* root) {
        if (!root) {
            return;
        }
        traversal(root->left);
        if (pre) {
            result = min(result, root->val - pre->val);
        }
        pre = root;
        traversal(root->right);
    }

};
中序遍历存放进数组
class Solution {
public:
    vector<int> path;
    int getMinimumDifference(TreeNode* root) {
        int result = INT_MAX;
        inorderTra(root);
        for (int i = 1; i < path.size(); i++) {
            result = min(result, path[i] - path[i - 1]);
        }  
        return result;
    }
    void inorderTra(TreeNode* root) {
        if (!root) {
            return;
        }
        inorderTra(root->left);
        path.push_back(root->val);
        inorderTra(root->right);

    }
};

LeetCode 501. 二叉搜索树中的众数

常规做法
class Solution {
public:
    unordered_map<int, int> map;

    bool static cmp(const pair<int, int>& l, const pair<int, int>& r) {
        return l.second > r.second;
    }   

    vector<int> findMode(TreeNode* root) {
        vector<int> res;
        inorderTra(root);
        vector<pair<int, int>> path(map.begin(), map.end());
        sort(path.begin(), path.end(), cmp);
        res.push_back(path[0].first);
        for(int i = 1; i < path.size(); i++) {
            if (path[i].second == path[0].second){
                res.push_back(path[i].first);
            }else {
                break;
            }
        }
        return res;

    }
    void inorderTra(TreeNode* root) {
        if (!root) {
            return ;
        }
        inorderTra(root->left);
        map[root->val]++;
        inorderTra(root->right);
    }
};
二叉搜索树中
class Solution {
public:
    int count = 0;
    int maxCount = 0;
    TreeNode* pre = nullptr;
    vector<int> res;
    void searchBST(TreeNode* root) {
        if (!root){
            return;
        }
        searchBST(root->left);
        if (!pre) {
            count = 1;
        }else if (pre->val == root->val){
            count++;
        }else {
            count = 1;
        }
        pre = root;
        if (count == maxCount) {
            res.push_back(root->val);
        }
    
        if (count > maxCount) {
            maxCount = count;
            res.clear();
            res.push_back(root->val);
        }
        searchBST(root->right);
    }
    vector<int> findMode(TreeNode* root) {
        searchBST(root);
        return res;
    }


};

LeetCode 236. 二叉树的最近公共祖先

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == nullptr || root == p || root == q) {
            return root;
        }
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p , q);
        if (left && right) {
            return root;
        } else if (!left) {
            return right;
        }
        return left;  
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值