代码随想录 二叉树部分题目

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

https://leetcode.cn/problems/minimum-absolute-difference-in-bst/submissions/

class Solution {
public:
    //变量min_diff:接受组西奥插值
    //可采用中序遍历:因为要比较两个数的插值 左中右
    //遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。
    
    //2.学会利用前元素
    int result = INT_MAX;
    TreeNode* pre = nullptr;
    void travsel(TreeNode* root){
        //中序遍历 左中右
        if(!root) return ;
        
        travsel(root->left);
        if(pre != nullptr){
            result = min(result,root->val - pre->val);
        }
        pre = root;
        travsel(root->right);
    }
    int getMinimumDifference(TreeNode* root) {
        travsel(root);
        return result;

    }
    
    
    // //1.将二叉搜索树转化为有序数组
    // vector<int> vec;
    // void get_vec(TreeNode* root){
    //     //中序遍历 左中右
    //     if(!root) return ;
    //     get_vec(root->left);
    //     vec.push_back(root->val);
    //     get_vec(root->right);
    // }

    // int getMinimumDifference(TreeNode* root) {
    //     // if(!root->left && !root->right) return nullptr;
    //     get_vec(root);
    //     int result = INT_MAX;
    //     for(int i = 1;i<vec.size();i++){
    //         result = min(abs(vec[i]-vec[i-1]),result);
    //     }
    //     return result;

    // }
};

2.二叉搜索书的众数

https://leetcode.cn/problems/find-mode-in-binary-search-tree/submissions/

2.1前继和中序遍历

class Solution {
public:
    //有序二叉树 采用中序遍历
    //1.需要记录重复节点的数值 及其频率 可采用unordered_mao<int,int>统计频率,然后转换为vector<pair<int,int>>比较大小偶啊
    
    //2.采用前继pre与当前节点比较,更新计数器count;
    //用maxcount记录最大count,一旦新的count大于maxcount,就清空之前的数组,将其加入数组
    int max_count =0;
    int count = 0;
    TreeNode* pre;
    void getMode(TreeNode* root,vector<int>& vec){
        //中序遍历
        if(root == nullptr) return ;
        
        //左
        getMode(root->left,vec);
        
        //中节点处理逻辑 与前继比较
        if(pre == nullptr){ //前继为空
            count = 1;
        }
        else if(pre->val == root->val){ //前继不为空 与当前节点相同
                count++;
            }
        else {      //前继不为空 与当前节点不同
            count = 1;
        }
        pre = root; //将处理过的节点设为前继

        //与maxcount比较
        if(count == max_count){ // 如果和最大值相同,放进result中
            vec.push_back(root->val);
        }
        else if(count > max_count){     // 如果计数大于最大值频率
            max_count = count; 
            vec.clear();    // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
            vec.push_back(root->val);   

        }
        
        //右
        getMode(root->right,vec);
        return ;
    }
    
    
    vector<int> findMode(TreeNode* root) {
        vector<int> vec;
        getMode(root,vec);
        return vec;


    }

};

2.2迭代法

class Solution {
public:
    //有序二叉树 采用中序遍历
    //1.需要记录重复节点的数值 及其频率 可采用unordered_mao<int,int>统计频率,然后转换为vector<pair<int,int>>比较大小偶啊
    
    //2.采用迭代法和前继 中序遍历 需要指针cur 和栈stack
    
    vector<int> findMode(TreeNode* root) {
        stack<TreeNode*> sta;
        TreeNode* cur = root;
        TreeNode* pre = nullptr;
        int count = 0;
        int max_count = 0;
        vector<int> vec;

        while(cur  || !sta.empty()){
            if(cur != nullptr){    
                sta.push(cur);
                cur =  cur->left;   //左
            }
            else{
                cur = sta.top();    //中
                sta.pop();

                //判断当前节点与前继的关系
                if(pre == nullptr) {
                    count = 1;
                }else if(pre->val == cur->val){
                    count++;
                }else{
                    count = 1;
                }
                
                //判断当前count与max_count的大小关系
                if(count == max_count) {
                    vec.push_back(cur->val);
                }
                
                if(count > max_count){
                    max_count = count;  //更新最大众数频率
                    vec.clear();
                    vec.push_back(cur->val);
                }

                pre = cur;
                cur = cur->right;
            }

        }
        return vec;

    }
};

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

https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree/

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        //寻找祖先 采用后续遍历
        //1.终止条件:节点为空或节点为要查找的节点
        if(root == p || root == q || root == NULL){
            return root;
        }
        //左
        TreeNode* left_result = lowestCommonAncestor(root->left,p,q);//注意我们是遍历整棵树
        //右
        TreeNode* right_result = lowestCommonAncestor(root->right,p,q);

        //中 处理逻辑
        if(left_result == NULL && right_result == NULL){
            return NULL;
        }else if(left_result == NULL && right_result != NULL){
            return right_result;
        }else if(left_result != NULL && right_result == NULL){
            return left_result;
        }else{
            return root;
        }

        return NULL;     
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值