当前位置:网站首页>37. serialized binary tree
37. serialized binary tree
2022-06-12 05:19:00 【Be your goat】
The finger of the sword Offer 37. Serialize binary tree
Ideas : Sequence traversal
serialize
- initialization : queue queue, result res
- Remove the node from the queue :
- If the node is empty , Print string "null,"
- If the node is not empty , Print corresponding numbers , Add the left and right child nodes queue
- Return value :res
deserialize
- initialization : queue queue, The root node root
- Get the current node from the queue cur
- Traversal string , If the current value is not empty , structure cur->left, And join the queue
- Traversal string , If the current value is not empty , structure cur->right, And join the queue
- Return root node
/**
* 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:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
string ans;
while(q.size()){
TreeNode* cur=q.front();
q.pop();
if(cur==nullptr){
ans+="null,";
}
else{
ans+=to_string(cur->val)+",";
q.push(cur->left);
q.push(cur->right);
}
}
return ans;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
int i=0,j=0;
while(j<data.size()&&data[j]!=',')++j;
string s=data.substr(i,j-i);
if(s=="null") return nullptr;
TreeNode* root=new TreeNode(stoi(s));
queue<TreeNode*> q;
q.push(root);
while(q.size()){
TreeNode* cur=q.front();
q.pop();
i=j+1;
j++;
while(j<data.size()&&data[j]!=',')++j;
string s=data.substr(i,j-i);
if(s!="null"){
cur->left=new TreeNode(stoi(s));
q.push(cur->left);
}
i=j+1;
j++;
while(j<data.size()&&data[j]!=',')++j;
s=data.substr(i,j-i);
if(s!="null"){
cur->right=new TreeNode(stoi(s));
q.push(cur->right);
}
}
return root;
}
};
// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
Time complexity O(n)
Spatial complexity O(n)
边栏推荐
- LabVIEW about TDMS and Binary Storage Speed
- Esp32-who face detection
- 2022“高考记忆” 已打包完成,请查收!
- Layer sublayer assigns values to the page elements of the parent layer to achieve the effect of transferring values to the page of the parent layer
- Chapter 1
- Overview of common classes
- JS to determine whether the tags of multiple classes are empty
- National land use data of 30m precision secondary classification
- Index fund summary
- Fundamentals of intensive learning openai gym environment construction demo
猜你喜欢

Introduction to audio alsa architecture

Introduction to MMS memory optimization of Hisilicon MPP service

4.3 模拟浏览器操作和页面等待(显示等待和隐式等待、句柄)

Accumulated temperature spatial distribution data, temperature distribution data, sunshine data, rainfall distribution, solar radiation data, surface runoff data, land use data, NPP data, NDVI data

Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort

Can‘t find a suitable configuration file in this directory or any parent. Error reporting and resolution

1008 color classification

Codec of ASoC framework driven by alsa

How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)

ShanMeng and Beijing Adoption Day start NFT digital collection public offering
随机推荐
[cjson] precautions for root node
12.26 exercise summary
New knowledge today
org. apache. ibatis. binding. BindingException: Invalid bound statement (not found)
【cjson】根节点注意事项
JS to determine whether the tags of multiple classes are empty
[backtracking] backtracking to solve subset problems
Drive safety coding & troubleshooting guide
Minigui3 runs on Hisilicon hi3520d/hi3531 platform
Transpiration and evapotranspiration (ET) data, potential evapotranspiration, actual evapotranspiration data, temperature data, rainfall data
Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
Understanding of day16 array create query static and dynamic array array array performance in memory
A complete set of installation procedures (for learning and communication only)
[GIS tutorial] ArcGIS for sunshine analysis (with exercise data download)
Realease package appears – missing type parameter
JS controls the display and hiding of tags through class
SQL transaction
MySQL5.7.21 Build For ARM
Static keyword and inheritance, polymorphic and special classes
What is thinking