【练习3】

1.将二叉搜索树转为排序的双向链表

在这里插入图片描述
(好久没看数据结构,忘完了,学习大佬的代码)

class Solution {
public:
    Node* pre=nullptr,*head=nullptr; //pre为每次遍历时的前一个节点,head记录头节点
    Node* treeToDoublyList(Node* root) {
        if(root==nullptr)return nullptr; //空树
        else{
            dfs(root); //得到pre和head
            head->left=pre; //根节点的左指针指向它的前驱
            pre->right=head;//前驱的右指针指向根节点
            return head;
        }
    }
    void dfs(Node* root){
        if(root==nullptr)return;//空节点直接返回
        else{
            dfs(root->left);//中序遍历,先左,后右
            if(pre)pre->right=root;//如果该节点的左子树不为空,那么将它的前驱右指针指向它
            else head=root;//如果该节点的左子树为空,它就已经为根节点了
            
            root->left=pre;//此时的根节点的左指针指向它的前驱
            pre=root;//以该节点的后驱作为新的根节点
            dfs(root->right);
        }
    }

在这里插入图片描述

2.序列化与反序列化二叉树

在这里插入图片描述

class Codec {
public:

    // 将二叉树转为字符串
    string serialize(TreeNode* root) {
        ostringstream res;
        if(root == nullptr) return "null"; // 修改此处,返回字符串 "null"
        else {
            queue<TreeNode*> que;
            que.push(root);
            TreeNode* temp = nullptr; // 修改此处,初始化为nullptr
            while(!que.empty()) {
                int len = que.size();
                while(len--) {
                    temp = que.front();
                    que.pop();
                    if(temp) {
                        res << temp->val; // 修改此处,不添加空格
                        que.push(temp->left);
                        que.push(temp->right);
                    } else {
                        res << "null"; // 修改此处,添加字符串 "null"
                    }
                    res << " "; // 添加空格,分隔节点值
                }
            }
            return res.str();
        }
    }

    // 重建二叉树
    TreeNode* deserialize(string data) {
        istringstream input(data);
        string temp;
        vector<TreeNode*> vec;
        while(input >> temp) {
            if(temp == "null") {
                vec.push_back(NULL);
            } else {
                vec.push_back(new TreeNode(stoi(temp)));
            }
        }
        int loc = 1;
        for(int i = 0; i < vec.size(); i++) {
            if(vec[i] == NULL) continue;
            if(loc < vec.size()) {
                vec[i]->left = vec[loc];
                loc = loc + 1;
            }
            if(loc < vec.size()) {
                vec[i]->right = vec[loc];
                loc = loc + 1;
            }
        }
        return vec[0];
    }
};

在这里插入图片描述

3.套餐内商品的排列顺序

在这里插入图片描述

class Solution {
public:
    vector<string> goodsOrder(string goods) {
        dfs(goods, 0);
        return res;
    }
private:
    vector<string> res;
    void dfs(string& input, int loc) { // 注意这里传入的是 input 的引用
        if (loc == input.size() - 1) { // 当 loc 到达字符串末尾时
            res.push_back(input); // 将当前字符串加入结果集
            return;
        }
        set<char> st; // 用于去重
        for (int i = loc; i < input.size(); i++) {
            if (st.find(input[i]) != st.end()) continue; // 如果当前字符已经在当前位置之前出现过,则跳过
            st.insert(input[i]); // 将当前字符加入到集合中
            swap(input[i], input[loc]); // 将当前字符与位置 loc 的字符交换
            dfs(input, loc + 1); // 递归处理下一个位置
            swap(input[i], input[loc]); // 恢复交换之前的状态,进行下一次尝试
        }
    }
};

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值