Tree Traversals - Hard Version
Given the partial results of a binary tree's traversals in in-order, pre-order, and post-order. You are supposed to output the complete results and the level order traversal sequence of the corresponding tree.
Input Specification:
Each input file contains one test case. For each case, a positive integer N (≤) is given in the first line. Then three lines follow, containing the incomplete in-order, pre-order and post-order traversal sequences, respectively. It is assumed that the tree nodes are numbered from 1 to N and no number is given out of the range. A -
represents a missing number.
Output Specification:
For each case, print in four lines the complete in-order, pre-order and post-order traversal sequences, together with the level order traversal sequence of the corresponding tree. The numbers must be separated by a space, and there must be no extra space at the beginning or the end of each line. If it is impossible to reconstruct the unique tree from the given information, simply print Impossible
.
Sample Input 1:
9
3 - 2 1 7 9 - 4 6
9 - 5 3 2 1 - 6 4
3 1 - - 7 - 6 8 -
Sample Output 1:
3 5 2 1 7 9 8 4 6
9 7 5 3 2 1 8 6 4
3 1 2 5 7 4 6 8 9
9 7 8 5 6 3 2 4 1
Sample Input 2:
3
- - -
- 1 -
1 - -
Sample Output 2:
Impossible
1 #include<bits/stdc++.h> 2 using namespace std; 3 int n,t,flag; 4 vector<int> v0,vp; 5 vector<vector<int> > vd; 6 vector<vector<int> > v; 7 vector<vector<int> > vr; 8 vector<list<int> > vl(2); 9 vector<list<int> > vle(2); 10 bool ip2p(int i1,int i2,int p1,int p2,int pr,int d) 11 { 12 int v11=v[1][p1]; 13 vp[pr]=v11; 14 if(v[2][pr]&&(v[2][pr]!=v11))return false; 15 int vds=vd.size(); 16 if(vds<=d) 17 { 18 vector<int> vdd(1,v11); 19 vd.emplace_back(vdd); 20 } 21 else vd[d].emplace_back(v11); 22 23 int i3=vr[0][v11-1]; 24 int lenl=i3-i1; 25 int lenr=i2-i3; 26 if(lenl) 27 { 28 if(lenr)return ip2p(i1,i3-1,p1+1,p1+lenl,pr-1-lenr,d+1)&&ip2p(i3+1,i2,p1+lenl+1,p2,pr-1,d+1); 29 else return ip2p(i1,i3-1,p1+1,p1+lenl,pr-1-lenr,d+1); 30 } 31 else 32 { 33 if(lenr) return ip2p(i3+1,i2,p1+lenl+1,p2,pr-1,d+1); 34 else return true; 35 } 36 } 37 void dfs() 38 { 39 if(flag==2) 40 return; 41 else if(!vle[0].empty()) 42 { 43 int i=vl[0].front(); 44 int ie=vle[0].front(); 45 vle[0].pop_front(); 46 do 47 { 48 int ii=vl[0].front(); 49 vl[0].pop_front(); 50 v[0][ie]=ii; 51 vr[0][ii-1]=ie; 52 dfs(); 53 v[0][ie]=0; 54 vr[0][ii-1]=-1; 55 vl[0].emplace_back(ii); 56 57 }while(i!=vl[0].front()); 58 vle[0].emplace_front(ie); 59 } 60 else if(!vle[1].empty()) 61 { 62 int i=vl[1].front(); 63 int ie=vle[1].front(); 64 vle[1].pop_front(); 65 do 66 { 67 int ii=vl[1].front(); 68 vl[1].pop_front(); 69 v[1][ie]=ii; 70 vr[1][ii-1]=ie; 71 dfs(); 72 v[1][ie]=0; 73 vr[1][ii-1]=-1; 74 vl[1].emplace_back(ii); 75 }while(i!=vl[1].front()); 76 vle[1].emplace_front(ie); 77 } 78 else 79 { 80 if(flag==2) 81 return; 82 else 83 { 84 if(ip2p(0,n-1,0,n-1,n-1,0)) 85 { 86 flag++; 87 if(flag==1) 88 { 89 for(int i=0;i<2;i++) 90 for(int j=0;j<n;j++) 91 v0.emplace_back(v[i][j]); 92 for(auto i:vp) 93 v0.emplace_back(i); 94 for(auto i:vd) 95 for(auto j:i) 96 v0.emplace_back(j); 97 } 98 } 99 vd.clear(); 100 } 101 } 102 } 103 104 bool dfs(int in,int pre,int post,int s) 105 { 106 if(!s) 107 return true; 108 else if(v[1][pre]||v[2][post+s-1]) 109 { 110 int tm=max(v[1][pre],v[2][post+s-1]); 111 if(v[1][pre]&&v[1][pre]!=tm) return false; 112 if(v[2][post+s-1]&&v[2][post+s-1]!=tm) return false; 113 int rt=vr[0][tm-1]; 114 int p1=v[1][pre]; 115 int p2=v[2][post+s-1]; 116 v[1][pre]=tm; 117 v[2][post+s-1]=tm; 118 if(rt<0) 119 { 120 for(int i=in;i<in+s;i++) 121 { 122 if(!v[0][i]) 123 { 124 int lenl=i-in; 125 int lenr=s-1-lenl; 126 if(lenl<0 || lenr<0)continue; 127 v[0][i]=tm; 128 vr[0][tm-1]=i; 129 if(dfs(in,pre+1,post,s-lenr-1)&&dfs(in+lenl+1,pre+1+lenl,post+lenl,s-lenl-1)) 130 return true; 131 v[0][i]=0; 132 vr[0][tm-1]=-1; 133 } 134 } 135 return false; 136 } 137 else 138 { 139 int lenl=rt-in; 140 int lenr=s-1-lenl; 141 if(lenl<0 || lenr<0) 142 { 143 v[1][pre]=p1; 144 v[2][post+s-1]=p2; 145 return false; 146 } 147 else if(dfs(in,pre+1,post,s-lenr-1)&&dfs(in+lenl+1,pre+1+lenl,post+lenl,s-lenl-1)) 148 return true; 149 else 150 { 151 v[1][pre]=p1; 152 v[2][post+s-1]=p2; 153 return false; 154 } 155 } 156 } 157 else 158 { 159 for(int i=in;i<in+s;in++) 160 { 161 int lenl=i-in; 162 int lenr=s-1-lenl; 163 164 if(lenl<0 || lenr<0) 165 { 166 v[1][pre]=0; 167 v[2][post+s-1]=0; 168 } 169 else if(!v[0][i]) 170 { 171 v[0][i]=t; 172 vr[0][t-1]=i; 173 v[1][pre]=t; 174 v[2][post+s-1]=t; 175 if(dfs(in,pre+1,post,s-lenr-1)&&dfs(in+lenl+1,pre+1+lenl,post+lenl,s-lenl-1)) 176 return true; 177 v[0][i]=0; 178 vr[0][t-1]=-1; 179 } 180 else 181 { 182 v[1][pre]=v[0][i]; 183 v[2][post+s-1]=v[0][i]; 184 if(dfs(in,pre+1,post,s-lenr-1)&&dfs(in+lenl+1,pre+1+lenl,post+lenl,s-lenl-1)) 185 return true; 186 } 187 v[1][pre]=0; 188 v[2][post+s-1]=0; 189 } 190 return false; 191 } 192 } 193 void levelorder(int in,int pre,int s,int d) 194 { 195 if(!s) return; 196 else 197 { 198 int tm=v[1][pre]; 199 int rt=vr[0][tm-1]; 200 int lenl=rt-in; 201 int lenr=s-1-lenl; 202 if(d>=(int)vd.size()) 203 { 204 vector<int> vt(1,tm); 205 vd.emplace_back(vt); 206 } 207 else 208 vd[d].emplace_back(tm); 209 levelorder(in,pre+1,s-lenr-1,d+1); 210 levelorder(in+lenl+1,pre+lenl+1,s-lenl-1,d+1); 211 } 212 } 213 214 int main() 215 { 216 // freopen("data.txt","r",stdin); 217 scanf("%d",&n); 218 v0.resize(n,0); 219 vp.resize(n,0); 220 v.resize(3,v0); 221 v0.clear(); 222 v0.resize(n,-1); 223 vr.resize(3,v0); 224 v0.clear(); 225 flag=0; 226 for(int i=0;i<3;i++) 227 { 228 for(int j=0;j<n;j++) 229 { 230 scanf("%d",&v[i][j]); 231 if(v[i][j]) 232 { 233 t=v[i][j]-1; 234 vr[i][t]=j; 235 } 236 else if(i<2) 237 vle[i].emplace_back(j); 238 } 239 if(i<2) for(int j=0;j<n;j++) 240 { 241 if(vr[i][j]<0) 242 vl[i].emplace_back(j+1); 243 } 244 } 245 if(n<100) 246 { 247 dfs(); 248 if(flag==1) 249 { 250 for(int i=0;i<4*n;i++) 251 printf("%d%s",v0[i],(i+1)%n?" ":"\n"); 252 } 253 else printf("Impossible"); 254 } 255 else 256 { 257 dfs(0,0,0,n); 258 for(int i=0;i<3*n;i++) 259 printf("%d%s",v[i/n][i%n],(i+1)%n?" ":"\n"); 260 levelorder(0,0,n,0); 261 printf("%d",vd[0][0]); 262 for(int i=1;i<(int)vd.size();i++) 263 for(int j=0;j<(int)vd[i].size();j++) 264 printf(" %d",vd[i][j]); 265 } 266 return 0; 267 }