|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | + |
| 5 | + // Definition for a binary tree node. |
| 6 | + struct TreeNode { |
| 7 | + int val; |
| 8 | + TreeNode *left; |
| 9 | + TreeNode *right; |
| 10 | + TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 11 | + }; |
| 12 | + |
| 13 | +class Codec { |
| 14 | +public: |
| 15 | + |
| 16 | + |
| 17 | + string serialize(TreeNode* root) // Encodes a tree to a single string |
| 18 | + { |
| 19 | + string s=""; |
| 20 | + if(root==NULL)//Wherever the root is NULL,simply add "X "; |
| 21 | + return "X "; |
| 22 | + s+=(to_string(root->val)+" ");//Add the value string,then space and got to left and then right |
| 23 | + s+=serialize(root->left); |
| 24 | + s+=serialize(root->right); |
| 25 | + return s; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + TreeNode* func(string data,int& ind)// Decodes your encoded data to tree. |
| 30 | + { |
| 31 | + if((data[ind]=='X')||(ind>=data.length()))//If the value at index is 'X',that implies the node was null |
| 32 | + { |
| 33 | + ind+=2; |
| 34 | + return NULL; |
| 35 | + } |
| 36 | + string sx=""; |
| 37 | + while((ind<data.length())&&(data[ind]!=' '))//To get the value at that node |
| 38 | + { |
| 39 | + sx+=data[ind]; |
| 40 | + ind++; |
| 41 | + } |
| 42 | + ind++; |
| 43 | + int temp1=stoi( sx ); |
| 44 | + TreeNode* nx=new TreeNode(temp1);//Add this node and process for left and right |
| 45 | + nx->left=func(data,ind); |
| 46 | + nx->right=func(data,ind); |
| 47 | + return nx; |
| 48 | + } |
| 49 | + |
| 50 | + TreeNode* deserialize(string data) { |
| 51 | + int ind=0; |
| 52 | + return func(data,ind);//Calling the function with indices |
| 53 | + } |
| 54 | +};//End of Codec Class |
| 55 | + |
| 56 | +void inorder(TreeNode* temp)//Inorder traversal of the tree |
| 57 | +{ |
| 58 | + if(temp==NULL) |
| 59 | + return ; |
| 60 | + inorder(temp->left); |
| 61 | + cout<<temp->val<<" "; |
| 62 | + inorder(temp->right); |
| 63 | + return ; |
| 64 | +} |
| 65 | + |
| 66 | +int main() |
| 67 | +{ |
| 68 | + int n; |
| 69 | + cin>>n;//Number of nodes in full binary tree. |
| 70 | + vector<int> v(n); |
| 71 | + for(int i=0;i<n;i++)//Taking input of the tree as a level-order-vector and make it full binary tree by adding value -1 at null values |
| 72 | + cin>>v[i]; |
| 73 | + vector<TreeNode* > temp(n); |
| 74 | + for(int i=0;i<n;i++)//Creating the nodes |
| 75 | + { |
| 76 | + if(v[i]==-1) |
| 77 | + temp[i]=NULL; |
| 78 | + else |
| 79 | + temp[i]=new TreeNode(v[i]); |
| 80 | + } |
| 81 | + for(int i=0;i<n;i++)//Creating the binary tree by adding links |
| 82 | + { |
| 83 | + if(temp[i]!=NULL) |
| 84 | + { |
| 85 | + temp[i]->left=temp[2*i+1]; |
| 86 | + temp[i]->right=temp[2*i+2]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Codec codec;//Object Creation |
| 91 | + TreeNode* head=codec.deserialize(codec.serialize(temp[0]));//Passing temp[0] as root parameter |
| 92 | + cout<<"Inorder Traversal is:"<<endl; |
| 93 | + inorder(head);//Checking whether the process went correct by printing the values of the tree in inorder fashion |
| 94 | + return 0; |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +//Eg input :- |
| 99 | +// 15 |
| 100 | +//1 2 3 -1 -1 5 4 -1 -1 -1 -1 -1 -1 -1 -1 |
| 101 | +//Output:- |
| 102 | +//Inorder Traversal is: |
| 103 | +//2 1 5 3 4 |
0 commit comments