0
点赞
收藏
分享

微信扫一扫

Leetcode297:二叉树的序列化与反序列化(广搜)

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.

Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.

Clarification: The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Constraints:

The number of nodes in the tree is in the range [0, 104].
-1000 <= Node.val <= 1000

思路:层序遍历二叉树,将值用空格分隔,再根据层序遍历序列,用广搜复原二叉树

/**
 * 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) {
        queue<TreeNode*> q;
        string singleString="";
        if(!root) return singleString;
        q.push(root);
        while(!q.empty()){
            auto q_pop=q.front();q.pop();
            if(q_pop==root){
                singleString=to_string(q_pop->val);
            }else{
                if(q_pop){
                    singleString+=" "+to_string(q_pop->val);
                }else{
                    singleString+=" #";
                    continue;
                }
            }
            q_pop->left?q.push(q_pop->left):q.push(nullptr);
            q_pop->right?q.push(q_pop->right):q.push(nullptr);
        }
        while(singleString.back()=='#'||singleString.back()==' '){
            singleString.pop_back();
        }
        cout<<singleString;
        return singleString;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        if(data=="") return nullptr;
        stringstream ss(data);
        string val;
        vector<string> nodeVals;
        while(ss>>val){
            nodeVals.push_back(val);
        }
        queue<TreeNode*> q;
        TreeNode* root=new TreeNode(stoi(nodeVals[0]));
        q.push(root);
        for(int i=1;i<nodeVals.size();i++){
            auto q_pop=q.front();q.pop();
            if(nodeVals[i]!="#"){
                q_pop->left=new TreeNode(stoi(nodeVals[i]));
                q.push(q_pop->left);
            }
            if(i+1<nodeVals.size()&&nodeVals[++i]!="#"){
                q_pop->right=new TreeNode(stoi(nodeVals[i]));
                q.push(q_pop->right);
            }            
        }
        return root;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
举报

相关推荐

0 条评论