代码随想录算法训练营第十八天|LeetCode530.二叉搜索树的最小绝对差、LeetCode501.二叉搜索树中的众数、LeetCode236. 二叉树的最近公共祖先

目录

前言

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

题目链接:

代码:

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

题目链接:

代码:

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

题目链接:

代码:


前言

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

文章讲解:代码随想录

视频讲解:LeetCode:530.二叉搜索树的最小绝对差_哔哩哔哩_bilibili

LeetCode501.二叉搜索树中的众数

文章讲解:代码随想录

视频讲解:LeetCode:501.二叉搜索树中的众数_哔哩哔哩_bilibili

LeetCode236. 二叉树的最近公共祖先

文章讲解:代码随想录

视频讲解:LeetCode:236. 二叉树的最近公共祖先_哔哩哔哩_bilibili


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

题目链接:

530. 二叉搜索树的最小绝对差 - 力扣(LeetCode)

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* pre = nullptr;
    int result = INT32_MAX;

    void traversal(TreeNode* root)
    {
        if(root == nullptr) return ;

        traversal(root->left);
        if(pre && root->val - pre->val < result)
        {
            result = root->val - pre->val;
        }
        pre = root;
        traversal(root->right);
    }

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

    }
};

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

题目链接:

501. 二叉搜索树中的众数 - 力扣(LeetCode)

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> result;
    int count, maxCount = 0;
    TreeNode* pre = nullptr;
    void traversal(TreeNode* root)
    {
        if(root == nullptr) return;

        traversal(root->left);
        if(pre == nullptr) 
        {
            count = 1;
        }
        else if(pre->val == root->val)
        {
            count++;
        }
        else
        {
            count = 1;
        }
        pre = root;
        if(maxCount == count)
        {
            result.push_back(root->val);
        }
        else if(maxCount < count)
        {
            maxCount = count;
            result.clear();
            result.push_back(root->val);
        }
        traversal(root->right);

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

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

题目链接:

236. 二叉树的最近公共祖先 - 力扣(LeetCode)

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr) return nullptr;
        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 root;
        }
        else if(left && !right)
        {
            return left;
        }
        else if(!left && right)
        {
            return right;
        }

        return nullptr;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值