代码随想录算法训练营第21天 [ 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先]

代码随想录算法训练营第21天 [ 530.二叉搜索树的最小绝对差 501.二叉搜索树中的众数 236. 二叉树的最近公共祖先]


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

链接: 代码随想录.
思路:用两个指针,关键是 pre = cur;
做题状态:看解析后做出来了

class Solution
{
public:
    int result = INT_MAX;
    TreeNode *pre = nullptr;
    int getMinimumDifference(TreeNode *root)
    {
        traversal(root);
        return result;
    }

    void traversal(TreeNode *cur)
    {
        if (cur == nullptr)
        {
            return;
        }
        traversal(cur->left);
        if (pre != nullptr)
        {
            result = abs(cur->val - pre->val) < result ? 
            abs(cur->val - pre->val) : result;
        }
        pre = cur;
        traversal(cur->right);
    }
};

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

链接: 代码随想录.
思路:方法1:遍历二叉树,用map记录,再对map排序(怎么对map中的value排序?) 方法2:因为是类二叉搜索树,所以众数肯定是二叉树连在一起的节点,用上一题类似的想法,两个指针
做题状态:看解析后做出来了

// 方法1 用map记录
 class Solution
 {
 public:
     unordered_map<int, int> map;
     bool static smp(pair<int, int> a, pair<int, int> b)
     {
         return a.second > b.second;
     }

    vector<int> findMode(TreeNode *root)
    {
        vector<int> result;
        travelsal(root);
        vector<pair<int, int>> vec(map.begin(), map.end());
        sort(vec.begin(), vec.end(), smp);
        for (auto i : vec)
        {
            if (i.second == vec[0].second)
            {
                result.push_back(i.first);
            }
        }
        return result;
    }

    void travelsal(TreeNode *cur)
    {
        if (cur == nullptr)
        {
            return;
        }
        travelsal(cur->left);
        map[cur->val]++;
        travelsal(cur->right);
    }
};
class Solution
{
public:
    TreeNode *pre = nullptr;
    int count = 0;
    int maxCount = 0;
    vector<int> result;
    vector<int> findMode(TreeNode *root)
    {
        pre = nullptr;
        count = 0;
        maxCount = 0;
        result.clear();

        travelsal(root);
        return result;
    }

    void travelsal(TreeNode *cur)
    {
        if (cur == nullptr)
        {
            return;
        }
        travelsal(cur->left);
        //一开始pre还没初始化,所以这时候count = 1
        if (pre == nullptr)
        {
            count = 1;
        }
        //找到了count++
        else if (pre->val == cur->val)
        {
            count++;
        }
        //如果值不同,conut重置为1
        else
        {
            count = 1;
        }
        //更新pre的值
        pre = cur;
        //如果count == maxCount,说明是众数,把结果加进来
        if (count == maxCount)
        {
            result.push_back(cur->val);
        }
        //说明现在找到的才是众数,之前的结果全部clear(),更新maxCount,同时加入结果
        else if (count > maxCount)
        {
            maxCount = count;
            result.clear();
            result.push_back(cur->val);
        }
        travelsal(cur->right);
        return;
    }
};

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

链接: 代码随想录.
思路:将左右节点的结果向上返回,用后序遍历
做题状态:看解析后做出来了

class Solution
{
public:
    TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q)
    {
        TreeNode *ans = traversal(root, p, q);
        return ans;
    }

    TreeNode *traversal(TreeNode *cur, TreeNode *p, TreeNode *q)
    {
        if (cur == nullptr)
        {
            return nullptr;
        }
        TreeNode *left = traversal(cur->left, p, q);
        TreeNode *right = traversal(cur->right, p, q);
        if (cur->val == p->val || cur->val == q->val)
        {
            return cur;
        }
        if (left && right)
        {
            return cur;
        }
        if (left && !right)
        {
            return left;
        }
        if (!left && right)
        {
            return right;
        }
        return nullptr;
    }
};
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值