void go(vector<int>& preorder, int pl, int pr, vector<int>& res,int rl,int rr)
{
if (pr - pl == 2)
{
res[rr] = preorder[pl];
res[rl] = preorder[pl + 1];
res[rl + 1] = preorder[pr];
return;
}
res[rr] = preorder[pl];
int pmid = (pr - pl) / 2 + pl, rmid = (rr - rl) / 2 + rl;
go(preorder, pl + 1, pmid, res,rl,rmid-1);
go(preorder, pmid + 1, pr, res,rmid,rr-1);
}
vector<int> pre2postorder(vector<int> preorder)
{
vector<int> res(preorder.size());
go(preorder, 0, preorder.size() - 1, res, 0, preorder.size() - 1);
return res;
}
int main()
{
vector<int> pre(15);
for (int i = 0; i < 15; i++)
pre[i] = i + 1;
vector<int> res = pre2postorder(pre);
for (int i = 0; i < 15; i++)
cout << res[i] << " ";
}