diff --git a/2 May Serialize and deserialize a binary tree b/2 May Serialize and deserialize a binary tree new file mode 100644 index 00000000..03db84e6 --- /dev/null +++ b/2 May Serialize and deserialize a binary tree @@ -0,0 +1,39 @@ +class Solution +{ + public: + //Function to serialize a tree and return a list containing nodes of tree. + void ino(Node *root, vector &v){ + if(root==NULL) return ; + ino(root->left,v); + v.push_back(root->data); + ino(root->right,v); + } + + vector serialize(Node *root) + { + vectorv; + ino(root,v); + + + return v; + } + + Node * deSel(vector &A,int &ind){ + if(ind >= A.size() || A[ind] == -1) return NULL; + + Node *root = new Node(A[ind++]); + root->right = deSel(A,ind); + root->left = deSel(A,ind); + + + return root; + } + + Node * deSerialize(vector &A) + { + //Your code here + int ind = 0; + return deSel(A,ind); + } + +};