数据结构 - 满二叉树前序序列转后序序列

void go(vector& preorder, int pl, int pr, vector& 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 pre2postorder(vector preorder)
{
	vector res(preorder.size());
	go(preorder, 0, preorder.size() - 1, res, 0, preorder.size() - 1);
	return res;
}
int main()
{
	vector pre(15);
	for (int i = 0; i < 15; i++)
		pre[i] = i + 1;
	vector res = pre2postorder(pre);
	for (int i = 0; i < 15; i++)
		cout << res[i] << " ";
} 

 

你可能感兴趣的:(note)