当前位置:网站首页>High frequency leetcode deep search part: 297 Serialization and deserialization of binary tree
High frequency leetcode deep search part: 297 Serialization and deserialization of binary tree
2022-07-22 09:28:00 【Hiccup~~~~】
297. Serialization and deserialization of binary trees
Difficulty 766
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 , You just need to make sure that a binary tree can be serialized into a string and that the string can be deserialized into the original tree structure .
Tips : Input output format and LeetCode The current usage is consistent , Please refer to LeetCode The format of serializing binary tree . You don't have to do this , You can also use other methods to solve this problem .
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]
Tips :
- The number of nodes in the tree is in the range [0, 104] Inside
- -1000 <= Node.val <= 1000
* Ideas
- Serialization follows the normal idea of traversing the tree , Supplemented by records
- The key to deserialization , Or recursion
- After root assignment , Recursive left and right subtrees
- Numbers , Strings are passed through stoi,getline,to_string Wait for the function to complete
Code
/** * 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:
TreeNode* buildTree(queue<string>& q){
if(q.front()=="X"){
q.pop();
return NULL;
}
TreeNode* root=new TreeNode( stoi(q.front()) );// Restore string to number
q.pop();
root->left=buildTree(q);
root->right=buildTree(q);
return root;
}
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
// Serialization is obtained by pre ordering , Serialization is separated by spaces
if(root==NULL){
return "X ";
}
string dat=to_string(root->val)+' ';
dat+=serialize(root->left);
dat+=serialize(root->right);
return dat;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
stringstream str(data);// Pass the string into the input stream ,getline( Input stream , character string , Spacer )
string node_val;
queue<string> q;
while (getline(str, node_val, ' '))// Extract each node
q.push(node_val);
TreeNode* root=buildTree(q);
return root;
}
};
// Your Codec object will be instantiated and called as such:
// Codec ser, deser;
// TreeNode* ans = deser.deserialize(ser.serialize(root));
边栏推荐
猜你喜欢
随机推荐
大批量数据excel下载—本文作者只试了51万数据的下载,用时7秒
1978-2021 China Statistical Yearbook, 2003-2019 (provincial and municipal panel data), 1999-2019 (county panel data)
Docker系列 八.Docker下 Mysql 启动慢查询日志
设备重启卡死问题分析-reboot卡死
1989-2020年学历结构和计算平均受教育年限数据
Leetcode1734: arrangement after decoding XOR
EL表达式的初步使用
高频leetcode深搜部分:112. 路径总和
Redis原理之BigKey和热点Key
【ACM/二分】二分清晰入门级讲解
接口文档进化图鉴,有些古早接口文档工具,你可能都没用过
Mathematical operation
Axure repeater
electron
二分法查找
队列(堆栈)
文献: Axure(简单介绍)
Byte stream
二维数组的使用(包括二维数组的定义,二维数组的声明和初始化(动态初始化,静态初始化),二位数组的常见赋值方法(动态初始化,静态初始化的赋值),错误的定义赋值方法等)
EasyCVR平台安全扫描提示Go pprof调试信息泄露的解决办法