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

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] << " ";
} 

 

    原文作者:满二叉树
    原文地址: https://blog.csdn.net/Runner_of_nku/article/details/82383478
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞