代码随想录算法训练营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;
    }
};
相关推荐
杭州杭州杭州2 小时前
pta考试
数据结构·c++·算法
Remember_9932 小时前
【数据结构】初识 Java 集合框架:概念、价值与底层原理
java·c语言·开发语言·数据结构·c++·算法·游戏
栈低来信2 小时前
klist链表
linux·数据结构·链表
Remember_9932 小时前
【数据结构】Java集合核心:线性表、List接口、ArrayList与LinkedList深度解析
java·开发语言·数据结构·算法·leetcode·list
better_liang2 小时前
Java技术栈中的MySQL数据结构应用与优化
java·数据结构·mysql·性能调优·索引优化
qq_589568103 小时前
centos6.8版本虚拟机使用过程中的问题解决
数据结构·centos·list·esc键盘
Flash.kkl3 小时前
递归、搜索与回溯算法概要
数据结构·算法
sin_hielo3 小时前
leetcode 3047
数据结构·算法·leetcode
天赐学c语言3 小时前
1.17 - 排序链表 && 虚函数指针是什么时候初始化的
数据结构·c++·算法·链表·leecode