LintCode-Serialize and Deserialize Binary Tree

Description

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binary tree is ‘deserialization’.

Example

An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:

  3
 / \
9  20
  /  \
 15   7

Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.

You can use other method to do serializaiton and deserialization.

  • 题目模板

    /**
    * Definition of TreeNode:
    * class TreeNode {
    * public:
    *     int val;
    *     TreeNode *left, *right;
    *     TreeNode(int val) {
    *         this->val = val;
    *         this->left = this->right = NULL;
    *     }
    * }
    */
    
    class Solution {
    public:
      /**
       * This method will be invoked first, you should design your own algorithm
       * to serialize a binary tree which denote by a root node to a string which
       * can be easily deserialized by your own "deserialize" method later.
       */
      string serialize(TreeNode * root) {
          // write your code here
      }
    
      /**
       * This method will be invoked second, the argument data is what exactly
       * you serialized at method "serialize", that means the data is not given by
       * system, it's given by your own serialize method. So the format of data is
       * designed by yourself, and deserialize it here as you serialize it in
       * "serialize" method.
       */
      TreeNode * deserialize(string &data) {
          // write your code here
      }
    };
    
  • 题目大意

    这道题说的很模糊,读了读给的代码才弄懂他到底想要做什么。他会以一个给你一个二叉树(TreeNode)类型,然你把这个二叉树存起来(线性化成string类型),然后用第二个函数把这个string类型读进去,恢复成原来的二叉树,返回二叉树的根节点(TreeNode类型)。

    输入输出的事例是以二叉树的先序遍历的形式给你的,便于调代码,真实情况不是那样的。

  • 大概思路

    二叉树线性化的题,还是很好写的,三种遍历方式,我选的是先序遍历,因为用的最多,熟悉。

    借助queue实现起来可以不用递归,每次把节点加进去,取出来的时候判断如果是空就输出#,否则就输出权值。这里用了ostringstream,这是一个输出流,感觉在这里很好用,不会用的可以百度,也可以看我代码,其实很简单的。

    二叉树反线性化也用了istringstream,借助queue实现不用递归,很简单。

    /**
    * Definition of TreeNode:
    * class TreeNode {
    * public:
    *     int val;
    *     TreeNode *left, *right;
    *     TreeNode(int val) {
    *         this->val = val;
    *         this->left = this->right = NULL;
    *     }
    * }
    */
    
    class Solution {
    public:
      /**
       * This method will be invoked first, you should design your own algorithm
       * to serialize a binary tree which denote by a root node to a string which
       * can be easily deserialized by your own "deserialize" method later.
       */
      string serialize(TreeNode * root) {
          // write your code here
          ostringstream out;
          TreeNode *t;
          queue<TreeNode*> q;
          if(root)
            q.push(root);
          while(!q.empty()){
            t = q.front();
            q.pop();
            if(t){
              out << t->val << " ";
              q.push(t->left);
              q.push(t->right);
            }else
              out << "# ";
          }
          return out.str();
      }
    
      /**
       * This method will be invoked second, the argument data is what exactly
       * you serialized at method "serialize", that means the data is not given by
       * system, it's given by your own serialize method. So the format of data is
       * designed by yourself, and deserialize it here as you serialize it in
       * "serialize" method.
       */
      TreeNode * deserialize(string &data) {
          // write your code here
          if(data.empty())
            return NULL;
          istringstream in(data);
          queue<TreeNode*> q;
          string val;
          in >> val;
          //atoi(data[i++])
          TreeNode *root = new TreeNode(stoi(val));
          TreeNode *t, *cur;
          q.push(root);
          while(!q.empty()){
            if (!(in >> val))
              break;
            cur = q.front();
            q.pop();
            if(val != "#"){
              t = new TreeNode(stoi(val));
              cur->left = t;
              q.push(t);
            }
    
            if (!(in >> val))
              break;
            if(val != "#"){
              t = new TreeNode(stoi(val));
              cur->right = t;
              q.push(t);
            }
          }
          return root;
      }
    };
    
  • 细节方面

    他有一组测试数据是给你空值,这里要注意判断一下,两边,线性化和反线性化都需要判断,在这里栽了一次。

  • 题目链接 https://www.lintcode.com/problem/serialize-and-deserialize-binary-tree/description

猜你喜欢

转载自blog.csdn.net/qq_24889575/article/details/81590976