Advertisement
imashutosh51

Serialize and Deserialize Binary Tree

Oct 25th, 2022
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10. /*
  11. Logic:
  12. step1: We will traverse the tree levelwise using BFS in serialize function and store in string.
  13. while stroing tree in string,we will give one space between every node and "null" for NULL node.
  14.  
  15. step2: In deserialize function,we will store the nodes into vector of string and pass to fun.
  16. fun will traverse the tree levelwise using BFS and push the elements into tree.
  17. */
  18. class Codec {
  19. public:
  20.     string serialize(TreeNode* root) {
  21.         if(!root) return "";
  22.         deque<TreeNode*> q;
  23.         q.push_back(root);
  24.         string res="";
  25.         while(!q.empty()){
  26.             int k=q.size();
  27.             for(int i=0;i<k;i++){
  28.                 TreeNode* temp=q.front();
  29.                 q.pop_front();
  30.                 if(!temp){
  31.                     res+="null ";
  32.                 }
  33.                 else{
  34.                     res+=to_string(temp->val);
  35.                     res+=" ";
  36.                     q.push_back(temp->left);
  37.                     q.push_back(temp->right);
  38.                 }
  39.             }
  40.         }
  41.         return res;
  42.        
  43.        
  44.     }
  45.     int _stoi(string s){
  46.         int ans=0,i=0;
  47.         bool sign=false;
  48.         if(s[i]=='-'){ sign=true;i++;}
  49.         while(i<s.size()){
  50.             ans=ans*10+(s[i++]-'0');
  51.         }
  52.         if(sign) ans=-ans;
  53.         return ans;
  54.     }
  55.     TreeNode* fun(vector <string> arr){
  56.         if(arr.size()==0) return NULL;
  57.         deque<TreeNode*> q;
  58.         TreeNode* res=new TreeNode(_stoi(arr[0]));
  59.         q.push_back(res);
  60.         int i=1;
  61.         while(!q.empty()){
  62.             int k=q.size();
  63.             for(int j=0;j<k;j++){
  64.                 TreeNode* temp=q.front();
  65.                 q.pop_front();
  66.                 if(arr[i]!="null"){
  67.                     temp->left=new TreeNode(_stoi(arr[i++]));
  68.                     q.push_back(temp->left);
  69.                 }
  70.                 else i++;
  71.                 if(arr[i]!="null"){
  72.                     temp->right=new TreeNode(_stoi(arr[i++]));
  73.                     q.push_back(temp->right);
  74.                 }
  75.                 else i++;
  76.             }
  77.         }
  78.         return res;
  79.     }
  80.    
  81.     TreeNode* deserialize(string s) {
  82.         if(s=="") return NULL;
  83.         vector <string> arr;
  84.         int i=0;
  85.         while(i<s.size()){
  86.             if(s[i]=='n'){ arr.push_back("null"); i+=5;}
  87.             else if(s[i]=='-' || isdigit(s[i])){   //number can be negative also
  88.                 bool sign=false;
  89.                 if(s[i]=='-'){ sign=true;i++;}
  90.                 string val="";
  91.                 while(isdigit(s[i])){
  92.                     val+=s[i++];
  93.                 }
  94.                 if(sign) val="-"+val;
  95.                 arr.push_back(val);
  96.                 i++;
  97.             }
  98.         }
  99.         return fun(arr);
  100.     }
  101. };
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement