当前位置:网站首页>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)
边栏推荐
- Field xxxxDAO in com. nero. hua. service. impl. LoginServiceImpl required a bean of type
- [backtracking] backtracking method to solve combinatorial problems
- Detailed usage of vim editor
- Realease package appears – missing type parameter
- Overview of common classes
- Multi thread learning 4. Sleep, wait, yield, join (), ThreadGroup control the running of threads
- New knowledge today
- The emergence of new ides and the crisis of programmers?
- JS how to get the date
- Quickly get PCA (principal component analysis) (principle code case)
猜你喜欢

2022“高考记忆” 已打包完成,请查收!

1009 word search

Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data

Thingsboard create RCP widget
![[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e](/img/2e/97310ec36aeb1fc1e9c82361141a36.jpg)
[getting to the bottom] five minutes to understand the combination evaluation model - fuzzy borde (taking the C question of the 2021 college students' numerical simulation national competition as an e

BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)

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

Simulation of array implementation stack

Ecosystem type distribution data, land use data, vegetation type distribution and nature reserve distribution data

Detailed tutorial on the use of yolov5 and training your own dataset with yolov5
随机推荐
Detailed explanation of data envelopment analysis (DEA) (taking the 8th Ningxia provincial competition as an example)
The most commonly used objective weighting method -- entropy weight method
Esp32-who face detection
Abstract methods and interfaces
MySQL5.7.21 Build For ARM
Longest palindrome string
2022“高考记忆” 已打包完成,请查收!
Caused by: org. h2.jdbc. JdbcSQLSyntaxErrorException: Table “USER“ already exists; SQL statement:
Common MySQL date query
JS to determine whether it is the first time to browse the web page
Index fund summary
4.3 simulate browser operation and page waiting (display waiting and implicit waiting, handle)
C asynchronous programming (async and await) and asynchronous method synchronous invocation
Sword finger offer30 days re brush
Introduction to MMS memory optimization of Hisilicon MPP service
1006 next spread
12.24 day exercise -- Programming summation, 99 multiplication table, while loop and for loop exercises
Stm32f4 ll library multi-channel ADC
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
JS set the position of the current scroll bar