题目描述

请实现两个函数,分别用来序列化和反序列化二叉树。
你需要设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
提示:输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
示例:

  1. 输入:root = [1,2,3,null,null,4,5]
  2. 输出:[1,2,3,null,null,4,5]

分析

分为两个步骤:

  1. 序列化:利用一个队列保存TreeNode,再利用一个count计算当前在队列中的指针不为空的数量,当count不为空时循环。
    1. 每一次取队头元素,如果队头元素不为nullptr,则将其中的val插入到string尾部并且count--,并且将其左右孩子指针入队;
    2. 每一次取队头元素,如果队头元素为nullptr,则在string尾部插入null;
    3. 队友元素出队,最后将末尾的”,”,进行切片清除。
  2. 反序列化:利用split,将字符串切片为vector<string>,然后循环,也是利用queue保存TreeNode指针,利用一个count记录当前结点的序号,如果序号为偶数则是左孩子,如果序号是奇数则是右孩子。对vector<string>进行循环遍历:

    1. 当vec[i]=='null'时,
      1. 如果当前序号为偶数,则取队头指针,将其左孩子赋值为null;
      2. 如果当前序号为奇数,则取队头指针,将其右孩子赋值为null ,并且队头元素出队。
    2. 当vec[i]!='null'时,

      1. 创建结点,并且使用vec[i]对结点p进行初始化;
      2. 如果当前序号为偶数,则取队头指针,将其左孩子赋值为p,p入队;
      3. 如果当前序号为偶数,则取队头指针,将其右孩子赋值为p,队头元素出队,p入队;

        答案

        ```cpp class Codec { public: // Encodes a tree to a single string. string serialize(TreeNode root) { if (root == nullptr){ return “”; } queue<TreeNode> q; string s;

        q.push(root); int count = 1; while (count != 0) { auto t = q.front(); if (t != nullptr) {

        1. s+=to_string(t->val);
        2. s.push_back(',');
        3. count--;
        4. if (t->left != nullptr) {
        5. count++;
        6. }
        7. if (t->right != nullptr) {
        8. count++;
        9. }
        10. q.push(t->left);
        11. q.push(t->right);

        } else {

           s += "null";
           s += ",";
        

        } q.pop(); } return s.substr(0, s.size() - 1); }

      // Decodes your encoded data to tree. TreeNode deserialize(string data) { queue<TreeNode> q; vector v_s = split(data, “,”); int count = 0; TreeNode* head = nullptr; for (auto& i : v_s) {

        if (i != "null") {
            TreeNode* p = new TreeNode(atoi(i.c_str()));
            count++;
            if (!q.empty()) {
                auto t = q.front();
                if (count % 2 == 0) {
                    t->left = p;
                }
                else {
                    t->right = p;
                    q.pop();
                }
            }
            else {
                head = p;
            }
            q.push(p);
        }else{
            count++;
            if (!q.empty()) {
                auto t = q.front();
                if (count % 2 == 0) {
                    t->left = nullptr;
                }
                else {
                    t->right = nullptr;
                    q.pop();
                }
            }
        }
      

      } return head; }

    private: std::vector split(const std::string& str, const std::string& pattern) {

     std::vector<std::string> resVec;
    
     if ("" == str) {
         return resVec;
     }
     //方便截取最后一段数据
     std::string strs = str + pattern;
    
     size_t pos  = strs.find(pattern);
     size_t size = strs.size();
    
     while (pos != std::string::npos) {
         std::string x = strs.substr(0, pos);
         resVec.push_back(x);
         strs = strs.substr(pos + 1, size);
         pos  = strs.find(pattern);
     }
    
     return resVec;
    

    } }; ```