DAY21|530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先

文章介绍了三个关于二叉搜索树的问题解决方案:一是找到树中节点值的最小绝对差;二是查找树中的众数(出现次数最多的节点值);三是确定给定两个节点的最近公共祖先。每个问题都通过递归或栈操作来遍历树并找到答案。
摘要由CSDN通过智能技术生成

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

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

/**
 * 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:
    int getMinimumDifference(TreeNode* root) 
    {
        int res=INT_MAX;
        TreeNode*pre=nullptr;
        TreeNode*cur=root;
        stack<TreeNode*>st;
        while(cur!=nullptr||!st.empty())
        {
            if(cur!=nullptr)
            {
                st.push(cur);
                cur=cur->left;


            }
            else
            {
                cur=st.top();
                st.pop();
                if(pre!=nullptr)
                {
                    res=min(res,cur->val-pre->val);   }
                      pre=cur;
                cur=cur->right;
             
              
            }
        }
return res;
    }
};

501.二叉搜索树中的众数 

501. 二叉搜索树中的众数

/**
 * 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:
 void searchBST(TreeNode*cur,unordered_map<int,int>&map)
 {
     if(cur==nullptr)
     return;
     map[cur->val]++;
     searchBST(cur->left,map);
     searchBST(cur->right,map);
 }
 bool static cmp(const pair<int,int>&a,const pair<int,int>&b)
 {
     return a.second>b.second;
 }
public:

    vector<int> findMode(TreeNode* root) 
    {
    unordered_map<int,int>map;
    vector<int>res;
    if(root==nullptr)
    return res;
    searchBST(root,map);
    vector<pair<int,int>>vec(map.begin(),map.end());
    sort(vec.begin(),vec.end(),cmp);
    res.push_back(vec[0].first);
    for(int i=1;i<vec.size();i++)
    {
        if(vec[0].second==vec[i].second)
        res.push_back(vec[i].first);
        else 
        break;
    }

return res;

    }
};

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

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

/**
 * 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(p==root||q==root)
        return root;
        TreeNode*left=lowestCommonAncestor(root->left,p,q);
        TreeNode*right=lowestCommonAncestor(root->right,p,q);
        if(left&&right)
        return root;
        if(!left&&!right)
        return nullptr;
        if(!left||!right)
        return

















        
        // if(root==nullptr)
        // return nullptr;
        // if(p==root||q==root)
        // return root;
        // TreeNode*left=lowestCommonAncestor(root->left);
        // TreeNode*right=lowestCommonAncestor(root->right);
        // if(!left&&!right)
        // {
        //      return nullptr;
        // }
        // if(left&&!right)
        // {
        //     return left;
        // }
        // if(right==nullptr)
        // return left;
        // if(left&&right)
        // {
        //     return root;
        // }
        
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值