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.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { if (root == null) { return "#!"; } String res = root.val + "!"; res += serialize(root.left); res += serialize(root.right); return res; } // Decodes your encoded data to tree. public TreeNode deserialize(String data) { String[] split = data.split("!"); LinkedList<String> queue = new LinkedList<>(); for (int i = 0; i < split.length; i++) { queue.offer(split[i]); } return reconPreOrder(queue); } private TreeNode reconPreOrder(LinkedList<String> queue) { // TODO Auto-generated method stub String poll = queue.poll(); if (poll.equals("#")) { return null; } TreeNode head = new TreeNode(Integer.valueOf(poll)); head.left = reconPreOrder(queue); head.right = reconPreOrder(queue); return head; } } // Your Codec object will be instantiated and called as such: // Codec codec = new Codec(); // codec.deserialize(codec.serialize(root));
相关推荐
Serialize and Deserialize Java 示例程序。简单来讲,它的数据格式与json类似,但是在存储时对数字、多字节字符、数组等都做了很多优化,减少了无用的字符,二进制格式,也保证不用字符化带来额外的存储空间的增加...
c c语言_leetcode题解之0297_serialize_and_deserialize_binary_tree
Serialize and Deserialize Binary Tree、298. Binary Tree Longest Consecutive Sequence等。这类题目通常要求读者使用树的各种操作来解决问题,例如树的遍历、查找、插入等操作。 4. 图类题目 图类题目是...
Deserialize Binary Tree Minimum Window Substring C++ STL中常见容器的时间复杂度 map, set, multimap, and multiset 上述四种容器采用红黑树实现,红黑树是平衡二叉树的一种。不同操作的时间复杂度近似为: 插入:O...
return 1 + Math.max(height(tree.leftChild()), height(tree.rightSibling())); } ``` #### 4. 序列化二叉树 序列化是指将数据结构或对象状态转换为可以存储或传输的形式的过程。对于二叉树来说,序列化可以帮助...
* [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...
本文实例讲述了C#序列化与反序列化(Serialize,Deserialize)实现方法。分享给大家供大家参考。具体分析如下: 如果要保存运行程序过程的数据要么保存到数据库中,要么新建一个普通的文件,然后把数据保存进去.但是这...
YamlBeans makes it easy to serialize and deserialize Java object graphs to and from YAML, a human-friendly data format. Replace XML and properties files with YAML for more expressive power (lists, ...
This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize and deserialize these data. 中文版请看这里。 Gradle compile '...
Serialize and deserialize JSON and XML data Perform encryption and signing to facilitate secure communication between parties Embed and use SQLite databases in your applications Use threads and ...
同时,`Deserialize`指令提供了反序列化功能,可以从BYTE数组中恢复原始的UDT或STRUCT数据,这样可以实现数据的完整生命周期管理,从创建、传输到解析。 为了更好地掌握这个功能,建议动手实践,尝试创建自己的UDT...
form-serialize-and-calculate.html
use serde :: {Serialize, Deserialize}; #[derive(Serialize, Deserialize, OpgModel)] #[serde(rename_all = "camelCase" )] #[opg( "Simple enum" )] enum SimpleEnum { Test, Another, Yay, } #[derive...
you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects. LINQ to JSON allows you to easily ...
Alice Bob+-----------------------------------+ +-----------------------------------+| #[derive(Serialize, Deserialize)] | | #[derive(Serialize, Deserialize)] || struct Message | | struct Message |+--...
在MATLAB中,`serialize`...正确使用`serialize`和`deserialize`,可以在各种复杂的MATLAB开发场景中提高效率和便利性。在实践中,要根据具体需求选择合适的数据序列化和反序列化策略,以确保数据的完整性和兼容性。
Json.NET Json.NET is a popular ... you don't have a class to serialize or deserialize to, or the JSON is radically different from your class and you need to manually read and write from your objects.