代码随想录算法训练营DAY18第六章 二叉树part06

目录

[530. 二叉搜索树的最小绝对差](#530. 二叉搜索树的最小绝对差)

[501. 二叉搜索树中的众数](#501. 二叉搜索树中的众数)

[236. 二叉树的最近公共祖先](#236. 二叉树的最近公共祖先)


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

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 {
    void dfs(TreeNode* root,int& pre,int& ans){
        if(!root)return ;
        dfs(root->left,pre,ans);
        if(pre==-1)pre=root->val;
        else {
            ans=min(ans,root->val-pre);
            pre=root->val;
        }
        dfs(root->right,pre,ans);
    }
public:
    int getMinimumDifference(TreeNode* root) {
        int ans=INT_MAX;
        int pre=-1;
        dfs(root,pre,ans);
        return ans;
    }
};

501. 二叉搜索树中的众数

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 {
    int maxnum=0;
    unordered_map<int,int>map;
    void search(TreeNode* node) {
        if(!node)return;
        map[node->val]++;
        maxnum=max(maxnum,map[node->val]);
        search(node->left);
        search(node->right);
    }
public:
    vector<int> findMode(TreeNode* root) {
        search(root);
        vector<int> ans;
        for(auto& [key,value]:map){
            if(value==maxnum)ans.push_back(key);
        }
        return ans;
    }
};

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

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||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;
        else return NULL;
    }
};
相关推荐
小飞学编程...5 分钟前
【哈希表】
数据结构·哈希算法·散列表
重生之后端学习3 小时前
283. 移动零[简单]✅
开发语言·数据结构·算法·leetcode·职场和发展
动词ing4 小时前
【C语言】结构体+文件基础
c语言·开发语言·数据结构
晚风醉蝶6 小时前
1-11-奇偶排序-OddEvenSort
java·数据结构·算法
旖旎夜光6 小时前
LeetCode 852:山脉数组的峰顶索引(二分查找) —— 题解
数据结构·c++·算法·leetcode·二分查找
小雪崩8 小时前
嵌入式学习 day25:哈希表及排序与查找
linux·c语言·数据结构·学习·排序算法
冻柠檬飞冰走茶11 小时前
《数据结构实验指导-C++语言版》 求单链表list中的元素个数,即表长
开发语言·数据结构·c++·算法·list
_Narcissus_11 小时前
动态规划初探(含完整代码实现)
c语言·数据结构·c++·算法·动态规划·背包问题·最短路径
_Narcissus_13 小时前
枚举和模拟算法笔记
c语言·数据结构·c++·笔记·算法·模拟·枚举
冻柠檬飞冰走茶13 小时前
《数据结构实验指导-C++语言版》 在顺序表 list 中查找元素 x
开发语言·数据结构·c++·算法·list