输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5

#include<vector>
#include<iostream>
using namespace std;

//参数是vector<int>  实现
// Definition for binary tree
struct TreeNode {
		int val;
		struct TreeNode *left;
		struct TreeNode *right;
		TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) 
{
		if(pre.size() == 0 || vin.size() == 0)
				return NULL;
		int rootValue = pre[0];
		TreeNode* root = new TreeNode(rootValue);
		if(pre.size() == 1&& vin.size() == 1 && vin[0] == pre[0])
				return root;
		vector<int> leftSubPre;
		vector<int> rightSubPre;
		vector<int> leftSubIn;
		vector<int> rightSubIn;
		vector<int>::iterator it = vin.begin();

		int leftSubTreeLength = 0;
		while(it != vin.end()&& rootValue != *it)
		{
				leftSubIn.push_back(*it);
				++it;
				++leftSubTreeLength;
		}
		++it;
		int rightSubTreeLength = 0;
		while(it != vin.end())
		{
				rightSubIn.push_back(*it);
				++it;
				++rightSubTreeLength;
		}
		it = pre.begin();
		++it;
		int i =1;
		for(;i<leftSubTreeLength+1;++i)
		{
				leftSubPre.push_back(pre[i]);
		}
		for(;i<pre.size();++i)
		{
				rightSubPre.push_back(pre[i]);
		}
		if(leftSubTreeLength > 0)
				root->left = reConstructBinaryTree(leftSubPre,leftSubIn);
		if(rightSubTreeLength > 0)
				root->right = reConstructBinaryTree(rightSubPre,rightSubIn);
	return root;
}
void showInOrder(TreeNode *root)
{
		if( root == NULL)
				return;
		showInOrder(root->left);
		cout<<root->val<<" 	";
		showInOrder(root->right);
}

int main()
{
		vector<int>	Pre;
		Pre.push_back(1);
		Pre.push_back(2);
		Pre.push_back(4);
		Pre.push_back(7);
		Pre.push_back(3);
		Pre.push_back(5);
		Pre.push_back(6);
		Pre.push_back(8);
		vector<int> In;
		In.push_back(4);
		In.push_back(7);
		In.push_back(2);
		In.push_back(1);
		In.push_back(5);
		In.push_back(3);
		In.push_back(8);
		In.push_back(6);
		TreeNode* root = reConstructBinaryTree(Pre,In);
		showInOrder(root);
		return 0;
}

//参数是一般的数组的实现
/*
   struct BinaryTreeNode
   {
   int  m_nValue;
   struct BinaryTreeNode* m_pLeft;
   struct BinaryTreeNode* m_pRight;
   BinaryTreeNode(int val):m_nValue(val),m_pLeft(NULL),m_pRight(NULL)
   {}
   };
   BinaryTreeNode* ConstructCore(int *startPreorder,int *endPreorder,int *startInorder,int *endInorder);
   BinaryTreeNode* Construct(int *preorder,int *inoder,int  length)
   {
   if(preorder == NULL ||inoder == NULL||length<=0)
   return NULL;
   return ConstructCore(preorder,preorder + length -1,inoder,inoder+length-1);
   }
   BinaryTreeNode* ConstructCore(int *startPreorder,int *endPreorder,int *startInorder,int *endInorder)
   {
   int rootValue = startPreorder[0];
   BinaryTreeNode* root = new BinaryTreeNode(rootValue);
   if(startPreorder == endPreorder && startInorder == endInorder && startInorder[0] == startPreorder[0])
   {
   return root;
   }
   int *rootInInoder = startInorder;
   while(rootInInoder != endInorder && rootValue != *rootInInoder)
   {
   ++rootInInoder;
   }

   int leftSubTreeLength = rootInInoder - startInorder;
   int rightSubTreeLength = endInorder - rootInInoder;
   if(leftSubTreeLength >0)
   {
   root->m_pLeft = ConstructCore(startPreorder+1,startPreorder+leftSubTreeLength,startInorder,rootInInoder-1);
   }
   if(rightSubTreeLength > 0)
   {
   root->m_pRight = ConstructCore(startPreorder+leftSubTreeLength+1,endPreorder,rootInInoder + 1,endInorder);
   }
   return root;

   }
   void showInOrder(BinaryTreeNode *root)
   {
   if( root == NULL)
   return;
   showInOrder(root->m_pLeft);
   cout<<root->m_nValue<<" 	";
   showInOrder(root->m_pRight);
   }
   int main()
   {
   int pre[] = {1,2,4,7,3,5,6,8};
   int in[] = {4,7,2,1,5,3,8,6};
   BinaryTreeNode* root = Construct(pre,in,8);
   showInOrder(root);
   cout<<endl;
   return 0;
   }
 */
/*
  
  
  
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值