Leetcode 297. Serialize and Deserialize Binary Tree

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.

For example, you may serialize the following tree

    1
   / \
  2   3
     / \
    4   5

as "[1,2,3,null,null,4,5]", just the same as how LeetCode OJ serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.

Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.

二叉树的序列化与反序列化。

其实就是通过某一种遍历方法将树变成序列存储,再将序列还原成树。用一个非数字的字符表示空节点,比如“#”

/**
 * 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:
    void dfs(string &path, TreeNode* root)
    {
        if(!root)
        {
            path += "#";
            return ;
        }
        path += to_string(root->val) + "#";
        dfs(path, root->left);
        dfs(path, root->right);
    }
    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string res;
        dfs(res, root);
        return res;
    }
    
    TreeNode* dfs2(string &data)
    {
        int pos = data.find("#");
        if(!pos)
        {
            data = data.substr(pos + 1);
            return NULL;
        }
        TreeNode* root = new TreeNode(stoi(data.substr(0, pos)));
        data = data.substr(pos + 1);
        root->left = dfs2(data);
        root->right = dfs2(data);
        return root;
    }
    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        string data2 = data;
        return dfs2(data2);
    }
};

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

20030
来自专栏菩提树下的杨过

无限级分类(非递归算法/存储过程版/GUID主键)完整数据库示例_(2)插入记录

-- ======================================== -- Author:  <杨俊明,jimmy.yang@cntvs.c...

21490
来自专栏SeanCheney的专栏

《Pandas Cookbook》第09章 合并Pandas对象

28110
来自专栏纯洁的微笑

平衡二叉树 AVL树结构详解 [Java实现]--源码部分

https://blog.csdn.net/zhang6622056/article/details/82698859

15810
来自专栏数据结构与算法

P2580 于是他错误的点名开始了

题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人。 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉...

32070
来自专栏五分钟学算法

LeeCode题目图解

There is an English version of README here. just click it!

18020
来自专栏码匠的流水账

聊聊storm TridentTopology的构建

storm-core-1.2.2-sources.jar!/org/apache/storm/trident/TridentTopology.java

12620
来自专栏前端说吧

正则表达式验证汇总

一、收集1  (转自https://blog.csdn.net/jumtre/article/details/13775351)

13710
来自专栏葡萄城控件技术团队

深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类、类成员函数、类成员变量进行访问控制。同时,访问控制符也是语法保留关键字,用于封装组件。 Pub...

35990
来自专栏扎心了老铁

java优雅的使用elasticsearch api

本文给出一种优雅的拼装elasticsearch查询的方式,可能会使得使用elasticsearch的方式变得优雅起来,使得代码结构很清晰易读。 建立elast...

1.2K70

扫码关注云+社区

领取腾讯云代金券