当前位置:网站首页>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 tool

What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation

Bug experience related to IAP jump of stm32

微信小程序自定义编译模式

图形管线基础(一)

万字归纳总结并实现各大常用排序及性能对比

【无标题】

Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和

Development of Quantitative Trading Robot System

气传导耳机哪个品牌比较好、这四款不要错过
随机推荐
动态规划--多步爬楼梯(爬楼梯进阶版)
Icc2 (IV) routing and postroute optimization
OJ 1045 反转然后相加
2022-05-24 use of spiel
NFT数藏盲盒+模式系统开发
execjs 调用
水渲染示例
What is the best and most cost-effective open headset recommended
藏宝计划TPC系统开发Dapp搭建
C语言的编译和预处理
valgrind工具
JSP should pass parameters to the background while realizing the file upload function
【动态规划--买卖股票的最佳时期系列3】
量化交易机器人系统开发
Use and safe shutdown of qthread thread in QT
OJ 1242 大一上之初出茅庐
什么气传导蓝牙耳机好、配置比较高的气传导耳机推荐
动态规划--简单题型之爬楼梯
Current learning progress
新的selenium