首页 > 其他分享 >lintcode:Binary Tree Serialization

lintcode:Binary Tree Serialization

时间:2022-12-01 19:05:24浏览次数:36  
标签:node Binary TreeNode string ss Tree serialize lintcode res


Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binary tree is ‘deserialization’.

There is no limit of how you deserialize or serialize a binary tree, you only need to make sure you can serialize a binary tree to a string and deserialize this string to the original structure.

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* This method will be invoked first, you should design your own algorithm
* to serialize a binary tree which denote by a root node to a string which
* can be easily deserialized by your own "deserialize" method later.
*/
void serializeHelper(TreeNode *node,string& res){
if(node){
res+=to_string(node->val)+" ";
}else{
res+="# ";
return;
}

serializeHelper(node->left,res);
/*
if(node->left){
serializeHelper(node->left,res);
}else{
res+="# ";
}*/

serializeHelper(node->right,res);
/*
if(node->right){
serializeHelper(node->right,res);
}else{
res+="# ";
}*/
}

string serialize(TreeNode *root) {
// write your code here

string res="";

serializeHelper(root,res);

return res;
}

/**
* This method will be invoked second, the argument data is what exactly
* you serialized at method "serialize", that means the data is not given by
* system, it's given by your own serialize method. So the format of data is
* designed by yourself, and deserialize it here as you serialize it in
* "serialize" method.
*/
TreeNode* deserializeHelper(istringstream& ss){
string str;
ss>>str;
if(str[0]=='#'){
return NULL;
}

int value=atoi(str.c_str());

TreeNode *node=new TreeNode(value);

node->left=deserializeHelper(ss);

node->right=deserializeHelper(ss);

return node;
}

TreeNode *deserialize(string data) {
// write your code here

istringstream ss(data);

TreeNode *root=deserializeHelper(ss);

return root;
}
};



标签:node,Binary,TreeNode,string,ss,Tree,serialize,lintcode,res
From: https://blog.51cto.com/u_15899184/5903603

相关文章

  • lintcode: N-Queens
    Then-queenspuzzleistheproblemofplacingnqueensonann×nchessboardsuchthatnotwoqueensattackeachother.Givenanintegern,returnalldistincts......
  • lintcode:Permutations
    Givenalistofnumbers,returnallpossiblepermutations.ChallengeDoitwithoutrecursion.1.递归classSolution{public:/***@paramnums:Alistofi......
  • lintcode:Subsets
    Givenasetofdistinctintegers,returnallpossiblesubsets.ChallengeCanyoudoitinbothrecursivelyanditeratively?1.18sclassSolution{public:/**......
  • lintcode: Permutations II
    Givenalistofnumberswithduplicatenumberinit.Findalluniquepermutations.可以见我的博文​​全排列实现​​classSolution{public:/***@paramnu......
  • lintcode: Subsets II
    Givenalistofnumbersthatmayhasduplicatenumbers,returnallpossiblesubsets1.先排序;再按求Subsets一样的做法,只是添加前检查是否已经存在。耗时171mscla......
  • lintcode:Subarray Sum Closest
    Givenanintegerarray,findasubarraywithsumclosesttozero.Returntheindexesofthefirstnumberandlastnumber.ExampleGiven[-3,1,1,-3,5],retur......
  • lintcode: Sqrt(x)
    Implementintsqrt(intx).Computeandreturnthesquarerootofx.Examplesqrt(3)=1sqrt(4)=2sqrt(5)=2sqrt(10)=3ChallengeO(log(x))求非线性方程的解可以......
  • stream流进行tree树组装
    1.准备工具类。packagecom.joolun.mall.util;importcom.joolun.mall.entity.TreeNode;importlombok.experimental.UtilityClass;importjava.util.ArrayList;im......
  • POJ - 1308 Is It A Tree?(并查集)
    POJ-1308IsItATree?(并查集)题目大意:传送门对于每一组测试样例,给出若干条无向边,判断由这些无向边构成的图是否为无环连通图题目分析:要点1:无环联通图(树)的性质:边......
  • 解决使用Sourcetree push代码,报错remote: The project you were looking for could no
    之前使用Sourcetreepush提交代码的时候遇到了下面的报错。第一时间排查确认了:1、develop分支是存在;2、我对这个项目也有提交的权限。进一步排查发现原因:发现这个......