Please implement two functions to serialize and deserialize binary trees respectively
The serialization of a binary tree refers to: saving the result of a binary tree according to a certain traversal method as a string in a certain format, so that the binary tree established in the memory can be persisted.
Serialization can be modified based on pre-order, in-order, post-order, and layer-by-layer binary tree traversal. The principle is the same (that is, the traversal order is different, and the processing of each node is the same), the result of serialization is a string, and the empty node (#) is represented by a certain symbol during serialization, with ! Indicates the end of a node's value (value!).
Deserialization of a binary tree refers to reconstructing the binary tree according to the serialized string result str obtained in a certain traversal order.
Reference method 1:
return (char*)res;
typedef TreeNode node; typedef TreeNode* pnode; typedef int* pint; class Solution { vector<int> buf; void dfs(pnode p){ if(!p) buf.push_back(0x23333); else{ buf.push_back(p -> val); dfs(p -> left); dfs(p -> right); } } pnode dfs2(pint& p){ if(*p == 0x23333){ ++p; return NULL; } pnode res = new node(*p); ++p; res -> left = dfs2(p); res -> right = dfs2(p); return res; } public: char* Serialize(TreeNode *p) { buf.clear(); dfs(p); int *res = new int[buf.size()]; for(unsigned int i = 0; i < buf.size(); ++i) res[i] = buf[i]; return (char*)res; //Convert int array to char* string } TreeNode* Deserialize(char *str) { int *p = (int*)str; return dfs2(p); } };Reference method 2: to_string() function
/* 1. For serialization: use preorder traversal, recursively convert the value of the binary tree into characters, and at each node of the binary tree When not empty, add a ' , ' as a split after the character obtained by converting val. For empty nodes, use '#' instead. 2. For deserialization: recursively use the characters in the string to create a binary tree in preorder order (note: When recursing, the parameter of the recursive function must be char **, so as to ensure that the pointer to the string after each recursion will be Move as the recursion progresses! ! ! ) */ class Solution { public: char* Serialize(TreeNode *root) { if(root == NULL) return NULL; string str; Serialize(root, str); char *ret = new char[str.length() + 1]; int i; for(i = 0; i < str.length(); i++){ ret [i] = str [i]; } ret [i] = '\ 0'; return ret; } void Serialize(TreeNode *root, string& str){ if(root == NULL){ str += '#'; return ; } string r = to_string(root->val); str + = r; str += ','; Serialize(root->left, str); Serialize(root->right, str); } TreeNode* Deserialize(char *str) { if(str == NULL) return NULL; TreeNode *ret = Deserialize(&str); return ret; } TreeNode* Deserialize(char **str){//Due to recursion, the string will be continuously read backwards if(**str == '#'){ //So be sure to use **str, ++(*str); //To ensure that the pointer str points to the unread character after recursion return NULL; } int num = 0; while(**str != '\0' && **str != ','){ num = num*10 + ((**str) - '0'); ++(*str); } TreeNode *root = new TreeNode(num); if(**str == '\0') return root; else (*str)++; root->left = Deserialize(str); root->right = Deserialize(str); return root; } };
https://blog.csdn.net/lzuacm/article/details/52704931
defined in the header file
std::string to_string( int value ); (1) (C ++11起)
std::string to_string( long value ); (2) (C ++11起)
std::string to_string( long long value ); (3) (C ++11起)
std::string to_string( unsigned value ); (4) (C ++11起)
std::string to_string( unsigned long value ); (5) (C ++11起)
std::string to_string( unsigned long long value ); (6) (C ++11起)
std::string to_string( float value ); (7) (C ++11起)
std::string to_string( double value ); (8) (C ++11起)
std::string to_string( long double value ); (9) (C ++11起)
std::to_string
is a feature introduced in the latest version of the C++ standard (2011). Older compilers may not support it.
1) Converting signed decimal integers to std::sprintf(buf, "%d", value) with the same string content yields a large enough one buf
.
2) Converting signed decimal integers to std::sprintf(buf, "%ld", value) with the same string content yields a large enough one buf
.
3) Converting signed decimal integers to std::sprintf(buf, "%lld", value) with the same string content yields a large enough buf
.
4) std::sprintf(buf, "%u", value)
will produce a string large enough to buf
convert the same content into an unsigned decimal integer.
5) std::sprintf(buf, "%lu", value)
will yield a string large enough to buf
convert the same content into an unsigned decimal integer.
6) std::sprintf(buf, "%llu", value)
will yield a string large enough to buf
convert the same content into an unsigned decimal integer.
6) std::sprintf(buf, "%llu", value)
will produce buf
a string of the same content large enough to convert to an unsigned decimal integer.
@ 7,8 @std::sprintf(buf, "%f", value)
will produce buf
a string of the same content large enough to convert to a floating point value.
9) std::sprintf(buf, "%Lf", value)
will yield a string large enough to buf
convert the same content to a floating point value.
parameter
value - a numeric conversion
return value
a string holding the converted value