【代码随想录算法训练营第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

相关推荐
smj2302_796826525 分钟前
解决leetcode第3777题使子字符串变交替的最少删除次数
python·算法·leetcode
Tisfy15 分钟前
LeetCode 2110.股票平滑下跌阶段的数目:数学(一次遍历)
数学·算法·leetcode·题解
1024小神16 分钟前
swift中 列表、字典、集合、元祖 常用的方法
数据结构·算法·swift
ULTRA??18 分钟前
Informed RRT*实现椭圆启发式采样
c++·算法
Swizard19 分钟前
告别样本不平衡噩梦:Focal Loss 让你的模型学会“划重点”
算法·ai·训练
亭台38 分钟前
【Matlab笔记_23】MATLAB的工具包m_map的m_image和m_pcolor区别
笔记·算法·matlab
李玮豪Jimmy39 分钟前
Day39:动态规划part12(115.不同的子序列、583.两个字符串的删除操作、72.编辑距离)
算法·动态规划
历程里程碑1 小时前
C++ 10 模板进阶:参数特化与分离编译解析
c语言·开发语言·数据结构·c++·算法
星辞树2 小时前
从 In-context Learning 到 RLHF:大语言模型的范式跃迁
算法
再__努力1点2 小时前
【68】颜色直方图详解与Python实现
开发语言·图像处理·人工智能·python·算法·计算机视觉