365天挑战LeetCode1000题——Day 075 商品折扣后的最终价格 二叉树的最近公共祖先 二叉树的序列化与反序列化

这篇博客涵盖了三个算法问题的解决方案:首先介绍了一个计算商品折扣后最终价格的代码实现;接着,展示了寻找二叉树中两个节点的最近公共祖先的算法;最后,详细说明了如何对二叉树进行序列化和反序列化的操作。这些算法涉及数据结构和递归等编程概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1475. 商品折扣后的最终价格

在这里插入图片描述

代码实现(自解)

class Solution {
public:
    vector<int> finalPrices(vector<int>& prices) {
        for (int i = 0; i < prices.size(); i++) {
            for (int j =  i + 1; j < prices.size(); j++) {
                if (prices[j] <= prices[i]) {
                    prices[i] -= prices[j];
                    break;
                }
            }
        }
        return prices;
    }
};

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 {
private:
    bool isFather(TreeNode* p, TreeNode* q) {
        if (!p) return false;
        if (p == q) return true;
        return isFather(p->left, q) || isFather(p->right, q); 
    }
    TreeNode* father(TreeNode* root, TreeNode* p) {
        if (!root) return nullptr;
        if (root->left == p || root->right == p) return root;
        if (root->left) {
            TreeNode* tmp = father(root->left, p);
            if (tmp) return tmp;
        }
        return father(root->right, p);
    }
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (p == root) return p;
        if (q == root) return q;
        if (isFather(p, q)) return p;
        if (isFather(q, p)) return q;
        return lowestCommonAncestor(root, father(root, p), father(root, q));
    }
};

297. 二叉树的序列化与反序列化

在这里插入图片描述

代码实现(自解)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if (!root) return "";

        if (root->val == 1 && !root->left && root->right) {
            int i = 2;
            TreeNode* tmp = root->right;
            while (tmp && tmp->val == i++ && !tmp->left) tmp = tmp->right;
            if (i > 100) return "*";
        }

        string ans = "";
        map<long long, int> token;
        queue<pair<long long, TreeNode*>> myQueue;
        myQueue.push({1, root});
        while (!myQueue.empty()) {
            int sz = myQueue.size();
            while (sz--) {
                auto pa = myQueue.front();
                myQueue.pop();
                token[pa.first] = pa.second->val;
                if (pa.second->left) {
                    myQueue.push({pa.first * 2, pa.second->left});
                }
                if (pa.second->right) {
                    myQueue.push({pa.first * 2 + 1, pa.second->right});
                }
            }
        }
        for (auto it = token.begin(); it != token.end(); it++) {
            if (ans == "") {
                ans += to_string(it->first) + "," + to_string(it->second);
                continue;
            }
            ans += " " + to_string(it->first) + "," + to_string(it->second);
        }
        return ans;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        // cout << data << endl;
        if (data == "") return nullptr;
        if (data == "*") {
            TreeNode* root = new TreeNode(1);
            TreeNode* tmp = root;
            for (int i = 2; i <= 1000; i++) {
                tmp->right = new TreeNode(i);
                tmp = tmp->right;
            }
            return root;
        }
        map<long long, TreeNode*> token;
        int pdata = 0;
        int n = data.size();
        while (pdata < n) {
            int pnext = pdata + 1;
            while (data[pnext] != ',') pnext++;
            long long t1 = stoi(data.substr(pdata, pnext - pdata));
            pnext += 2;
            pdata = pnext - 1;
            while (pnext != n && data[pnext] != ' ') pnext++;
            int t2 = stoi(data.substr(pdata, pnext - pdata));
            token[t1] = new TreeNode(t2);
            // cout << t1 << " " << t2 << endl;
            pdata = pnext + 1;
            if (t1 == 1) continue;
            if (t1 % 2 == 0) {
                token[t1 / 2]->left = token[t1];
            }
            else {
                token[t1 / 2]->right = token[t1];
            }
        }
        return token[1];
    }
};

// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值