【代码随想录算法训练营第37期 第二十一天 | LeetCode530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先】

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


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

解题代码C++:

cpp 复制代码
/**
 * 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 {
private:
    int result = INT_MAX;
    TreeNode* pre = NULL;
    void traversal(TreeNode* cur)
    {
        if(cur == NULL) return;

        traversal(cur->left);

        if(pre != NULL)
            result = min(result, cur->val - pre->val);
        pre = cur;

        traversal(cur->right);
    }

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

题目链接/文章讲解/视频讲解:
https://programmercarl.com/0530.二叉搜索树的最小绝对差.html



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

解题代码C++:

cpp 复制代码
/**
 * 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 {
private:
    int maxCount = 0;
    int count = 0;
    TreeNode* pre = NULL;
    vector<int> result;
    void searchBST(TreeNode* cur)
    {
        if(cur == NULL) return;
        
        searchBST(cur->left);

        if(pre == NULL)
            count = 1;
        else if(pre->val == cur->val)
            count ++;
        else
            count = 1;
        pre = cur;

        if(count == maxCount)
            result.push_back(cur->val);
        
        if(count > maxCount)
        {
            maxCount = count;
            result.clear();
            result.push_back(cur->val);
        }

        searchBST(cur->right);

        return;
    }

public:
    vector<int> findMode(TreeNode* root) {
        count = 0;
        maxCount = 0;
        pre = NULL;
        result.clear();

        searchBST(root);
        return result;
    }
};

题目链接/文章讲解/视频讲解:
https://programmercarl.com/0501.二叉搜索树中的众数.html



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

解题代码C++:

cpp 复制代码
/**
 * 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 == q || root == p || root == NULL) return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if(left != NULL && right != NULL) return root;
        if(left == NULL) return right;
        return left;
    }
};

题目链接/文章讲解/视频讲解:
https://programmercarl.com/0236.二叉树的最近公共祖先.html

相关推荐
行然梦实24 分钟前
论文阅读:《多目标和多目标优化的回顾与评估:方法和算法》
论文阅读·算法·机器学习·数学建模
castro31 分钟前
斐波那契堆:理论强者与现实挑战——深入解析高效优先队列的实现与局限
算法
go546315846535 分钟前
离散扩散模型在数独问题上的复现与应用
线性代数·算法·yolo·生成对抗网络·矩阵
惜鸟40 分钟前
PDF页眉页脚识别与去除方案
后端·算法
设计师小聂!1 小时前
力扣热题100-------74.搜索二维矩阵
算法·leetcode·矩阵
小O的算法实验室2 小时前
2025年ESWA SCI1区TOP,强化学习多目标灰狼算法MOGWO-RL+分布式混合流水车间调度,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
菥菥爱嘻嘻3 小时前
力扣面试150(44/150)
javascript·leetcode·面试
liulilittle3 小时前
备忘录设计模式 vs 版本设计模式
开发语言·c++·算法·设计模式
菜鸟555553 小时前
常用算法思想及模板
算法·dp·模板·分治·竞赛·算法思想
肆佰.3 小时前
c++ 派生类
数据结构·c++·算法