代码随想录算法训练营day21| 530.二叉搜索树的最小绝对差,501.二叉搜索树中的众数,236. 二叉树的最近公共祖先

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

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

双指针,创建一个新节点pre用于存放前一个节点,便于在遍历的过程中进行比较

仍是中序遍历,需要改变的就是中间的过程。

/**
 * 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 ans = INT_MAX;
    TreeNode* pre = NULL;
public:

    void traversal(TreeNode* cur){
        if(cur==NULL) return;

        //中序
        traversal(cur->left);//左

        if(pre!=NULL) ans = min(ans, cur->val - pre->val); //中
        pre = cur;

        traversal(cur->right); //右
        return;
    }
    int getMinimumDifference(TreeNode* root) {
        traversal(root);
        return ans;
    }
};

501.二叉搜索树中的众数

​​​​​​​501. 二叉搜索树中的众数

1.非二叉搜索树的方法

通过遍历统计每个数出现的频率,然后在对频率进行排序,将最高的取出。

使用map记录频率,由于map无法对value排序,需要额外建立vector来对频率排序

/**
 * 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:
    //非二叉搜索树
    void search(TreeNode* root, unordered_map<int, int>& mymap){
        if(root==NULL) return;
        mymap[root->val]++;
        search(root->left, mymap);
        search(root->right, mymap);
    }
    //降序
    bool static cmp(const pair<int, int>& pair1, const pair<int, int>& pair2){
        return pair1.second > pair2.second;
    }
    vector<int> findMode(TreeNode* root) {
        vector<int> ans;
        unordered_map<int, int> mymap;
        search(root, mymap);

        //map无法对value排序,需要转化为vector
        vector<pair<int, int>> vec(mymap.begin(), mymap.end());
        sort(vec.begin(), vec.end(), cmp);
        ans.push_back(vec[0].first);

        for(int i=1; i<vec.size(); i++){
            if(vec[0].second == vec[i].second) ans.push_back(vec[i].first);
            else break;
        }
        return ans;
    }
};

2.双指针法

/**
 * 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 count = 0;
    int maxcount = 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(maxcount == count){
            result.push_back(cur->val);
        }
        if(maxcount < count){
            maxcount = count; 
            result.clear();
            result.push_back(cur->val);
        }

        searchBST(cur->right); //右
        return;
    }
    vector<int> findMode(TreeNode* root) {
        searchBST(root);
        return result;
    }
};

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==NULL) return root;
        if(root==p || root==q) return root;

        TreeNode* findleft = lowestCommonAncestor(root->left, p ,q);
        TreeNode* findright = lowestCommonAncestor(root->right, p ,q);

        if(findleft!=NULL && findright!=NULL){
            return root;
        }
        else if(findleft==NULL){
            return findright;
        }
        return findleft;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值