输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。

假设输入的前序遍历和中序遍历的结果中都不含有重复的数字。 例如,前序遍历序列:{1,2,4,5,6},中序遍历序列:{3,2,4,1,6,5}

在二叉树的前序遍历序列中,第一个数字总是根节点的值。在中序遍历序列中,根节点的值在中间,左子树的值在根节点值的左边,右子树的值在根节点的值的右边。

#include<iostream>
using namespace std;

template<class T>
struct BinaryTreeNode
{
	T _value; //数据
	BinaryTreeNode<T>* _left; //左孩子
	BinaryTreeNode<T>* _right; //右孩子

	BinaryTreeNode()
		: _value()
		, _left(NULL)
		, _right(NULL)
	{}
	
};
template<class T>
class BinaryTree
{
public:	
	BinaryTreeNode<T>* ConstructCore(int* startPre,int* endPre,int* startIn,int* endIn)
	{
		int rootValue = startPre[0];  //前序遍历的第一个数字就是根节点的值
		BinaryTreeNode<T>* root = new BinaryTreeNode<T>(); 
		root->_value = rootValue;
		root->_left = root->_right = NULL;
		cout << root->_value<< " ";

		if (startPre == endPre)
		{
			if (startIn == endIn && *startPre == *startIn)
			{
				return root;
			}
			else
			{
				return NULL;
			}
		}

		int* rootIn = startIn;
		//在中序遍历中找根节点的值
		while (*rootIn != rootValue && rootIn <= endIn)
		{
			++rootIn;
		}
		if (rootIn == endIn && *rootIn != rootValue)
			return NULL;
		
		int leftLength = rootIn - startIn;//左子树长度   中序遍历序列中根节点前面的数字都是左子树节点的值
		int* leftEnd = startPre + leftLength;//前序遍历序列中左子树的值
		//构建左子树
		if (leftLength > 0)
		{
			root->_left=ConstructCore(startPre+1,leftEnd,startIn,rootIn-1);
		}
		//构建右子树
		if (leftLength < endPre - startPre)
		{
			root->_right = ConstructCore(leftEnd+1,endPre,rootIn+1,endIn);
		}
		return root;	
	}
	BinaryTreeNode<T>* Construct(int* preorder, int* inorder, int length)
	{
		if (preorder == NULL || inorder == NULL || length <= 0)
			return NULL;
		return ConstructCore(preorder,preorder+length-1,inorder,inorder+length-1);
	}	
};

int main()
{
	int preorder[] = { 1,2,4,7,3,5,6,8 };
	int inorder[] = { 4,7,2,1,5,3,8,6 };
	BinaryTree<int> bt;
	BinaryTreeNode<int>* root=bt.Construct(preorder, inorder, 8);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值