Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update solution_code.md #1483

Closed
wants to merge 1 commit into from
Closed

Conversation

Mungeryang
Copy link

Fixes

我修改的是如下题目的 xx 解法:

通过截图如下:

@Mungeryang
Copy link
Author

我修改的是如下题目的 LCR 048. 二叉树的序列化与反序列化](https://leetcode.cn/problems/h54YBf/)解法:

class Codec {
public:
    // Serialize the tree to a string
    string serialize(TreeNode* root) {
        ostringstream out;
        serialize(root, out);
        return out.str();
    }

    // Deserialize the string to a tree
    TreeNode* deserialize(string data) {
        istringstream in(data);
        return deserialize(in);
    }

private:
    // Helper function to serialize the tree
    void serialize(TreeNode* root, ostringstream& out) {
        if (root) {
            out << root->val << ' ';
            serialize(root->left, out);
            serialize(root->right, out);
        } else {
            out << "# ";
        }
    }

    // Helper function to deserialize the tree
    TreeNode* deserialize(istringstream& in) {
        string val;
        in >> val;
        if (val == "#") {
            return nullptr;
        }

        TreeNode* root = new TreeNode(stoi(val));
        root->left = deserialize(in);
        root->right = deserialize(in);

        return root;
    }
};

通过截图如下:
抱歉东哥,我不太会插图哎...

@Mungeryang Mungeryang closed this by deleting the head repository Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant