day21打卡

day21打卡

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

  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    int ans = INT_MAX;
    TreeNode* pre = nullptr;
    int getMinimumDifference(TreeNode* root) {
        dfs(root);
        return ans;
    }
    void dfs(TreeNode* root)
    {
        //递归出口
        if(root == nullptr) return;
        //中序遍历
        //左
        dfs(root->left);
        //中
        if(pre != nullptr)
        {
            ans = min(ans, root->val - pre->val);
        }
        //记录前一个节点
        pre = root;
        //右
        dfs(root->right);
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    int getMinimumDifference(TreeNode* root) {
        stack<TreeNode*> st;
        int ans = INT_MAX;
        TreeNode* cur = root;
        TreeNode* pre = nullptr;
        while(!st.empty() || cur != nullptr)
        {
            if(cur != nullptr)
            {
                st.push(cur);
                cur = cur->left;
            }
            else
            {
                cur = st.top();
                st.pop();
                 if(pre != nullptr)
                {
                    ans = min(ans, cur->val - pre->val);
                }
                pre = cur;
                cur = cur->right;
            }
        }
        return ans;
    }
};

501. 二叉搜索树中的众数

哈希表+遍历二叉搜索树

class Solution {
public:
    unordered_map<int, int> hash;
    struct cmp
    {
        bool operator()(const pair<int, int>& l, const pair<int, int>& r)
        {
            return l.second > r.second;
        }
    } cmp;
    vector<int> findMode(TreeNode* root) {
        vector<int> ret;
        if(root == nullptr) return ret;
        dfs(root);
        vector<pair<int, int>> tmp(hash.begin(), hash.end());
        sort(tmp.begin(), tmp.end(), cmp);
        for(int i = 0; i < tmp.size(); i++)
        {
            if(tmp[0].second == tmp[i].second)
            {
                ret.push_back(tmp[i].first);
            }
            else break;
        }
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if(root == nullptr) return;
        hash[root->val]++;
        dfs(root->left);
        dfs(root->right);
        return;
    }
};
  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    vector<int> ans;
    TreeNode* pre = nullptr;
    int count = 0, maxCount = 0;
    vector<int> findMode(TreeNode* root) {
        dfs(root);
        return ans;
    }
    void dfs(TreeNode* root)
    {
        if(root == nullptr) return;
        dfs(root->left);
        if(pre == nullptr)
        {
            count = 1;
        }
        else if(pre->val == root->val)
        {
            count++;
        }
        else if(pre->val != root->val)
        {
            count = 1;
        }
        pre = root;
        
        if(count == maxCount)
        {
            ans.push_back(root->val);
        }
        if(count > maxCount)
        {
            maxCount = count;
            ans.clear();
            ans.push_back(root->val);
        }
        dfs(root->right);
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    vector<int> findMode(TreeNode* root) {
        stack<TreeNode*> st;
        TreeNode* cur = root;
        TreeNode* pre = nullptr;
        int count = 0, maxCount = 0;
        vector<int> ans;
        while(!st.empty() || cur != nullptr)
        {
            if(cur != nullptr)
            {
                st.push(cur);
                cur = cur->left;
            }
            else
            {
                cur = st.top();
                st.pop();
                if(pre == nullptr)
                {
                    count = 1;
                }
                else if(pre->val == cur->val)
                {
                    count++;
                }
                else if(pre->val != cur->val)
                {
                    count = 1;
                }

                if(count == maxCount)
                {
                    ans.push_back(cur->val);
                }
                if(count > maxCount)
                {
                    maxCount = count;
                    ans.clear();
                    ans.push_back(cur->val);
                }
                pre = cur;
                cur = cur->right;
            }
        }
        return ans;
    }
};

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

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr || root == p || root == q) return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if(left == nullptr) return right;
        if(right == nullptr) return left;
        return root;
    }
};
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值