/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */#include<string>#include<iostream>usingnamespacestd;structInfo{TreeNode*node;intidx;};classCodec{public:// Encodes a tree to a single string.stringserialize(TreeNode*root){//preorder traversalstringresult;//serialize rootif(root){result+=to_string(root->val)+",";}else{result+="#,";returnresult;}//traverse leftresult+=serialize(root->left);//traverse rightresult+=serialize(root->right);returnresult;}// Decodes your encoded data to tree.TreeNode*deserialize(stringdata){_str=data;if(data=="")returnnullptr;// if root is null, then return// if root is not null and left is null, then new node of root// returndeserializeInternal(0).node;}InfodeserializeInternal(intidx){// process rootInforootInfo=getNode(idx);if(!rootInfo.node){returnrootInfo;}printf("node: %d\n",rootInfo.node->val);printf("idx: %d\n",rootInfo.idx);// process leftInfoleftInfo=deserializeInternal(rootInfo.idx);rootInfo.node->left=leftInfo.node;// process rightInforightInfo=deserializeInternal(leftInfo.idx);rootInfo.node->right=rightInfo.node;InforeturnInfo;returnInfo.node=rootInfo.node;returnInfo.idx=rightInfo.idx;returnreturnInfo;}InfogetNode(intidx){chara[6]={0,};inti=0;while(true){if(_str[idx+i]==','||_str[idx+i]==NULL)break;a[i]=_str[idx+i];i++;}Infoinfo;if(_str[idx]=='#'){info.node=nullptr;}else{info.node=newTreeNode(atoi(a));}info.idx=idx+i+1;returninfo;}private:string_str;};// Your Codec object will be instantiated and called as such:// Codec ser, deser;// TreeNode* ans = deser.deserialize(ser.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */#include<string>#include<iostream>usingnamespacestd;structInfo{TreeNode*node;intidx;};classCodec{public:voidserializeHelper(TreeNode*root){//preorder traversal//serialize rootif(root){_str+=to_string(root->val)+" ";}else{_str+="# ";return;}//traverse leftserializeHelper(root->left);//traverse rightserializeHelper(root->right);}// Encodes a tree to a single string.stringserialize(TreeNode*root){serializeHelper(root);return_str;}// Decodes your encoded data to tree.TreeNode*deserialize(stringdata){_str=data;if(data=="")returnnullptr;// if root is null, then return// if root is not null and left is null, then new node of root// returndeserializeInternal(0).node;}InfodeserializeInternal(intidx){// process rootInforootInfo=getNode(idx);if(!rootInfo.node){returnrootInfo;}// process leftInfoleftInfo=deserializeInternal(rootInfo.idx);rootInfo.node->left=leftInfo.node;// process rightInforightInfo=deserializeInternal(leftInfo.idx);rootInfo.node->right=rightInfo.node;InforeturnInfo;returnInfo.node=rootInfo.node;returnInfo.idx=rightInfo.idx;returnreturnInfo;}InfogetNode(intidx){chara[6]={0,};inti=0;while(true){if(_str[idx+i]==' '||_str[idx+i]==NULL)break;a[i]=_str[idx+i];i++;}Infoinfo;if(_str[idx]=='#'){info.node=nullptr;}else{info.node=newTreeNode(atoi(a));}info.idx=idx+i+1;returninfo;}private:string_str;};// Your Codec object will be instantiated and called as such:// Codec ser, deser;// TreeNode* ans = deser.deserialize(ser.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */#include<string>#include<iostream>usingnamespacestd;structInfo{TreeNode*node;intidx;};classCodec{public:voidserializeHelper(TreeNode*root){//preorder traversal//serialize rootif(root){_str+=to_string(root->val)+" ";}else{_str+="# ";return;}//traverse leftserializeHelper(root->left);//traverse rightserializeHelper(root->right);}// Encodes a tree to a single string.stringserialize(TreeNode*root){serializeHelper(root);return_str;}// Decodes your encoded data to tree.TreeNode*deserialize(stringdata){_iss=istringstream(data);returndeserializeInternal();}TreeNode*deserializeInternal(){// process rootstringw;_iss>>w;if(w=="#")returnnullptr;TreeNode*root=newTreeNode(stoi(w));// process leftroot->left=deserializeInternal();// process rightroot->right=deserializeInternal();returnroot;}private:string_str;istringstream_iss;};// Your Codec object will be instantiated and called as such:// Codec ser, deser;// TreeNode* ans = deser.deserialize(ser.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classCodec{public:// Encodes a tree to a single string.stringserialize(TreeNode*root){stringstreamss;serialize_helper(root,ss);returnss.str();}voidserialize_helper(TreeNode*root,stringstream&ss){if(!root)ss<<"# ";else{ss<<root->val<<" ";serialize_helper(root->left,ss);serialize_helper(root->right,ss);}}// Decodes your encoded data to tree.TreeNode*deserialize(stringdata){stringstreamss(data);returndeserialize_helper(ss);}TreeNode*deserialize_helper(stringstream&ss){stringval="";ss>>val;if(val=="#")returnnullptr;TreeNode*root=newTreeNode(stoi(val));root->left=deserialize_helper(ss);root->right=deserialize_helper(ss);returnroot;}};// Your Codec object will be instantiated and called as such:// Codec ser, deser;// TreeNode* ans = deser.deserialize(ser.serialize(root));
이것보다 좀 더 최적화 하려면, stringstream을 직접 for loop 돌게 하면 됩니다.