代码随想录训练营打卡第十八天

代码随想录: 代码随想录

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 {
private:
    unordered_map<int,int> mp;
    void traversal(TreeNode* root) {
        queue<TreeNode*> q;
        if (root != nullptr) q.push(root);
        while (!q.empty()) {
            int size = q.size();
            while (size--) {
                TreeNode* node = q.front();
                mp[node->val]++;
                if (node->left != nullptr) q.push(node->left);
                if (node->right != nullptr) q.push(node->right);
                q.pop();
            }
        }
    }
public:
    vector<int> findMode(TreeNode* root) {
        traversal(root);
        vector<int> ans;
        int maxCount = 0;
        for (auto p : mp) {
            maxCount = max(maxCount, p.second);
        }
        for (auto p : mp) {
            if (p.second == maxCount) {
                ans.push_back(p.first);
            }
        }
        return ans;
    }
};

二叉搜索树的方法:

image.png

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 traversal(TreeNode* root, unordered_map<int,int>& mp) {
        queue<TreeNode*> q;
        if (root != nullptr) q.push(root);
        while (!q.empty()) {
            int size = q.size();
            while (size--) {
                TreeNode* node = q.front();
                mp[node->val]++;
                if (node->left != nullptr) q.push(node->left);
                if (node->right != nullptr) q.push(node->right);
                q.pop();
            }
        }
    }
public:
    vector<int> findMode(TreeNode* root) {
		    unordered_map<int,int> mp;
        traversal(root, mp);
        vector<int> ans;
        int maxCount = 0;
        for (auto p : mp) {
            maxCount = max(maxCount, p.second);
        }
        for (auto p : mp) {
            if (p.second == maxCount) {
                ans.push_back(p.first);
            }
        }
        return ans;
    }
};

双指针:

/**
 * 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 curCount = 0, maxCount = 0;
    TreeNode* pre = nullptr;
    vector<int> res;
    void traversal(TreeNode* cur) {
        if (cur == nullptr)
            return;
        traversal(cur->left);
        if (pre == nullptr)
            curCount = 1;
        else if (pre->val == cur->val) {
            curCount++;
        } else {
            curCount = 1;
        }
        pre = cur;
        if (curCount == maxCount) {
            res.push_back(cur->val);
        } else if (curCount > maxCount) {
            maxCount = curCount;
            res.clear();
            res.push_back(cur->val);
        }
        traversal(cur->right);
    }

public:
    vector<int> findMode(TreeNode* root) {

        traversal(root);
        return res;
    }
};

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值