Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1.  
  2. void dfs( Node *root,vector<int>&list)
  3. {
  4.     if( root==NULL )
  5.     {
  6.         list.push_back(-1);
  7.         return;
  8.     }
  9.    
  10.    
  11.     list.push_back( root->data );
  12.    
  13.     dfs( root->left );
  14.     dfs( root->right );
  15. }
  16.  
  17. vector<int> serialize(Node *root)
  18. {
  19.    
  20.     vector<int>list;
  21.        
  22.     dfs( root,list );
  23.    
  24.     //5  3 -1 -1 2 6 -1 -1 1 -1 -1
  25.    
  26.     return list;
  27. }
  28.  
  29.  
  30. void totree( Node* root,int &pos,vector<int>&list )
  31. {
  32.    
  33.     if( list[pos]==-1 )
  34.     {
  35.         pos++;
  36.         return;
  37.     }
  38.    
  39.     root= new Node();
  40.     root->data=list[pos];
  41.    
  42.     pos++;
  43.     totree( root->left,pos,list );
  44.     totree( root->right,pos,list );
  45.    
  46. }
  47.  
  48. Node *deserialize(vector<int> list)
  49. {
  50.    
  51.    
  52.     Node *root;
  53.     root=NULL;
  54.     //5  3 -1 -1 2 6 -1 -1 1 -1 -1
  55.     totree( root,0,list );
  56.    
  57.    
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement