Sword Finger Offer 37-Serialized Binary Tree C++

Second question of debt repayment

Title description

Insert picture description here

Solution BFS

I won't say much about BFS. Personally, the difficulty of this question is the processing between string and int.
First put a blog post C, C++ string summary to see the character summary.
This is the second time I have encountered ostringstream / istringstream. It is a very useful two input stream. The str() function of ostringstream can separate the one with a space. The serial numbers are converted to comma-separated strings
** The usage of isringstream in this question is still the same as that encountered in leetcode-58 : each string separated by commas in the large string is used as a new string Processing, the
format is probably like this

 		istringstream in(data);
        string tmp;
        while(in >> tmp) {
    
    
        //do something
        }

I also saw the stoi function: you can convert a string to a number. If you exceed the upper limit of int, you will get an error. More detailed knowledge
about stoi and atoi
about c++ will be broken one by one during this winter vacation. After all, the interview is not just about writing a function. Fighting is much more than algorithms

/**
 * 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) {
    
    
        ostringstream out;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()) {
    
    
            TreeNode* tmp = q.front();
            q.pop();
            if(tmp != NULL) {
    
    
                out<<tmp -> val<<" ";
                q.push(tmp -> left);
                q.push(tmp -> right);
            }
            else out<<"null ";
        }
        return out.str();
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
    
    
        istringstream in(data);
        string tmp;
        vector<TreeNode*> a;
        while(in >> tmp) {
    
    
            if(tmp =="null") {
    
    
                a.push_back(NULL);
            }
            //stoi 把字符串转为int
            else a.push_back(new TreeNode(stoi(tmp)));
        }
        int index = 1;
        for(int i = 0; index < a.size(); i ++) {
    
    
            if(a[i] == NULL) continue;
            if(index < a.size()) a[i] -> left = a[index ++];
            if(index < a.size()) a[i] -> right= a[index ++];
        }
        return a[0];
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

Insert picture description here
Time complexity I don’t know how much ostringstream and isringstream are... Leave a hole in
space complexity

Guess you like

Origin blog.csdn.net/qq_42883222/article/details/112984375