LintCode-Serialize and Deserialize Binary Tree

本文介绍了一个算法的设计,该算法能够将二叉树序列化为字符串,并能从该字符串中反序列化回原来的二叉树结构。通过使用队列而非递归的方法,实现了先序遍历的序列化及反序列化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值