码迷,mamicode.com
首页 > 其他好文 > 详细

Construct binary tree form postorder and inorder--LeetCode

时间:2015-03-30 11:24:10      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:leetcode   算法   c++   

和上面的题目相似 思路也是类似

Tree* helpersecond(vector<int>& inorder,int in_begin,int in_end,vector<int>& post,int post_begin,int post_end)
{
	Tree* root =NULL;
	int mid;
	int i;
	if(in_begin > in_end)
	{
		return NULL;
	}
	else
	{
		for(i=in_begin;i<=in_end;i++)
			if(inorder[i] == post[post_end])
				break;
		if(i > in_end)
			return NULL;
		root = new Tree;
		root->value = post[post_end];
		root->left = helpersecond(inorder,in_begin,i-1,post,post_begin+i,post_end-1);
		root->right  =helpersecond(inorder,i+1,in_end,post,post_begin,post_begin+i-1);
	}
}

Tree* createBinTree(vector<int>& inorder,vector<int>& post) 
{
	if(inorder.size()==0 || post.size()==0)
		return NULL;
	return helpersecond(inorder,0,inorder.size()-1,post,0,post.size()-1);
}


Construct binary tree form postorder and inorder--LeetCode

标签:leetcode   算法   c++   

原文地址:http://blog.csdn.net/yusiguyuan/article/details/44746939

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!