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

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

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

题目链接

提交代码

class Solution {
public:
    TreeNode* pre = nullptr;
    int result = INT_MAX;
    void traversal(TreeNode* node)
    {
        if(node == nullptr) return;
        traversal(node -> left);
        if(pre != nullptr)
        {
            if(abs(node -> val - pre -> val) < result)
                result = abs(node -> val - pre -> val);
        }          
        pre = node;
        traversal(node -> right);
    }
    int getMinimumDifference(TreeNode* root) {
        TreeNode* cur = root;
        traversal(root);
        return result;
    }
};

提交代码(二分法)

class Solution {
public:
    vector<int> inorder;
    void traversal(TreeNode* node)
    {
        if(node == nullptr) return;
        traversal(node -> left);
        inorder.push_back(node -> val);
        traversal(node -> right);
    }
    int getMinimumDifference(TreeNode* root) {
        traversal(root);
        int result = INT_MAX;
        for(int i = 1; i < inorder.size(); i++)
        {
            if(abs(inorder[i] - inorder[i - 1]) < result)
                result = abs(inorder[i] - inorder[i - 1]);
        }
        return result;
    }
};

501.二叉搜索树中的众数

题目链接

提交代码(方法)

class Solution {
public:
    vector<int> result;
    int max_count = 0;
    int count = 0;
    TreeNode* pre = nullptr;
    void traversal(TreeNode* node)
    {
        if(node == nullptr) return;
        traversal(node -> left);
        if(pre)
        {
            if(node -> val == pre -> val)
                count++;
            else
                count = 1;
            if(count > max_count)
                {
                    result.clear();
                    result.push_back(node -> val);
                    max_count = count;
                }
            else if(count == max_count)
                    result.push_back(node -> val);
              
        }
        else
        {
            count++;
            result.push_back(node -> val);
            max_count = count;
        }
        pre = node;
        traversal(node -> right);
    }
    vector<int> findMode(TreeNode* root) {
        traversal(root);
        return result;
    }
};

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

题目链接

提交代码(方法)

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == p || root == q || root == nullptr) return root;
        TreeNode* left = lowestCommonAncestor(root -> left, p, q);
        TreeNode* right = lowestCommonAncestor(root -> right, p ,q);
        if(left != nullptr && right != nullptr) return root;
        else if(left != nullptr && right == nullptr) return left;
        else if(left == nullptr && right != nullptr) return right;
        return nullptr;
    }
};

总结

                     日期: 2023 年 3 月 15 日
              学习时长: 1 h 0 m
                     难度: ★ \bigstar ★ \bigstar
累计完成题目数量: 57
距离目标还有数量: 243
                      小结:

你可能感兴趣的:(c++)