代码随想录刷题第21天|LeetCode530二叉搜索树的最小绝对差、LeetCode501二叉搜索树中的众数、LeetCode236二叉树的最近公共祖先

1、LeetCode530 二叉搜索树的最小绝对差

题目链接:530、二叉搜索树的最小绝对差

class Solution {
public:
    int result = INT_MAX;
    TreeNode* pre = NULL;
    void traversal(TreeNode* root)
    {
        if (root == NULL) return;

        traversal(root->left);
        if (pre != NULL)
        {
            result = min(result, root->val - pre->val);
        }
        pre = root;

        traversal(root->right);
    }

    int getMinimumDifference(TreeNode* root) {
        traversal(root);
        return result;
    }
};

2、LeetCode501 二叉搜索树中的众数

题目链接:501、二叉搜索树中的众数

普通二叉树中的众数:

把树都遍历了,用map统计频率,把频率排序,最后取前面高频的元素的集合。

bool static compare(const pair<int,int>& a, const pair<int,int>& b);

class Solution {
public:
    void traversal(TreeNode* root, unordered_map<int,int>& map)
    {
        if (root == NULL) return;
        map[root->val]++;
        traversal(root->left, map);
        traversal(root->right, map);
    }

    bool static compare(const pair<int,int>& a, const pair<int,int>& b)
    {
        return a.second > b.second;
    }

    vector<int> findMode(TreeNode* root) {
        unordered_map<int,int> map;
        vector<int> result;
        if (root == NULL) return result;
        traversal(root, map);
        vector<pair<int,int>> vec(map.begin(), map.end());
        sort(vec.begin(), vec.end(), compare);
        result.push_back(vec[0].first);
        for (int i = 1; i < vec.size(); i++)
        {
            if (vec[i].second == vec[0].second) result.push_back(vec[i].first);
            else break;
        }

        return result;
    }
};

二叉搜索树中的众数:

双指针,if (pre->val == root->val) count++;

if (count == maxCount) result.push_back(root->val);

if (count > maxCount)  result.clear(); maxCount = count; result.push_back(root->val);

class Solution {
public:
    TreeNode* pre = NULL;
    vector<int> result;
    int count = 0;
    int maxCount = 0;

    void traversal(TreeNode* root)
    {

        if (root == NULL) return;
        traversal(root->left);

        if (pre == NULL)
        {
            count = 1;
        }
        else if (pre->val == root->val)    // 与前一个节点数值相同
        {
            count++;
        }
        else
        {
            count = 1;
        }
        pre = root;

        if (count == maxCount)             // 如果和最大值相同,放进result中
        {
            result.push_back(pre->val);
        }

        if (count > maxCount)
        {
            result.clear();   // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
            maxCount = count;
            result.push_back(pre->val);
        }

        traversal(root->right);

    }

    vector<int> findMode(TreeNode* root) {
        result.clear();
        traversal(root);
        return result;
    }
};

3、LeetCode236 二叉树的最近公共祖先

题目链接:236、二叉树的最近公共祖先

理解了挺久的,二刷再好好做一遍

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == NULL) 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!=NULL && right != NULL) return root;
        else if (left==NULL && right != NULL) return right;
        else if (left != NULL && right == NULL) return left;
        else
        {
           return NULL; 
        } 
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值