当前位置:网站首页>Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree
Leetcode brush question diary sword finger offer II 048. serialization and deserialization binary tree
2022-07-28 06:39:00 【JETECHO】
Original link (https://leetcode.cn/problems/h54YBf/)
List of articles
Title Description
Serialization is the operation of converting a data structure or object into consecutive bits , Then the transformed data can be stored in a file or memory , It can also be transmitted to another computer environment through the network , Reconstruct the original data in the opposite way .
Please design an algorithm to realize the serialization and deserialization of binary tree . There's no limit to your sequence / Deserialization algorithm execution logic , It is only necessary to ensure that a binary tree can be serialized into a string and deserialize the string into the original tree structure .
Example 1

Input :root = [1,2,3,null,null,4,5]
Output :[1,2,3,null,null,4,5]
Example 2
Input :root = []
Output :[]
Example 3
Input :root = [1]
Output :[1]
Example 4
Input :root = [1,2]
Output :[1,2]
Data constraints
- The number of nodes in the tree is in the range [0, 104] Inside
- -1000 <= Node.val <= 1000
Ideas
For serializing binary trees, the most natural way to think of is which traversal methods of binary trees —— The first sequence traversal , In the sequence traversal , After the sequence traversal , Here we use preorder traversal to construct a string , For each node, add its corresponding number , Use at the same time "null" Filling a binary tree makes it a “ Full binary tree ”, Use... In construction String Of split Function decomposes it into an array of characters , Then use the queue to store each node , Let one node out of the queue every time , Meet for "null" The node of is naturally given to that node null that will do .
Such as Example 1 It will become 
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Codec {
public String serialize(TreeNode root) {
return serialize(root, "");
}
public String serialize(TreeNode root, String str) {
if (root == null) {
str += "null,";
} else {
String tmp = root.val + ",";
str += tmp;
str = serialize(root.left, str);
str = serialize(root.right, str);
}
return str;
}
// Decodes your encoded data to tree.
public TreeNode deserialize(String data) {
String[] nodes=data.split(",");
Queue<String> queue=new LinkedList<>(Arrays.asList(nodes));
return deserialize(queue);
}
public TreeNode deserialize(Queue<String> queue) {
String val=queue.poll();
if(val.equals("null")) {
return null;
}
TreeNode node=new TreeNode(Integer.parseInt(val));
node.left=deserialize(queue);
node.right=deserialize(queue);
return node;
}
}
边栏推荐
猜你喜欢
随机推荐
夹子套利/搬砖套利系统开发
valgrind工具
Icc2 use report_ Placement check floorplan
空气传导耳机哪个牌子好、备受好评的气传导耳机推荐
用c语言实现三子棋小游戏
QT implementation outputs relevant information to log file
Execjs call
Solve the problem that the memory occupation is higher than that of the application process
【详解如何一步步实现三子棋】
【动态规划--买卖股票的最佳时期系列3】
2022-06-07 六.日志实现
What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation
2022-05-15 基于jwt令牌token
1、 Ffmpeg record audio as PCM file
OJ 1253 ordering problem
OJ 1129 分数矩阵
QT painting event - set background picture
What is hash? (development of Quantitative Trading Robot System)
2022-06-07 responsebodyadvice caused the spring frame problem in swagger
二维数组实战:螺旋矩阵






![[untitled]](/img/de/746832bfb3bb79b090215b135b8917.jpg)


