根据二叉树的前序遍历和中序遍历结果重建出该二叉树
2014-04-03 10:35
246 查看
struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode* m_nLeft;
BinaryTreeNode* m_nRight;
};
BinaryTreeNode* 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);
}
BinaryTreeNode* ConstructCore(int* startPreorder,int* endPreorder,int* startInorder,int* endInorder){
int rootValue=startPreorder[0]; //第一个值为根节点
BinaryTreeNode* root=new BinaryTreeNode();
root->m_nValue=rootValue;
root->m_nLeft=root->m_nRight=NULL; //构建节点
if(startPreorder=endPreorder){
if(startInorder=endInorder&&*startPreorder=*startInorder) //只有一个结点的时候
return root;
else
throw exception("Invalid Input");
}
int* rootInorder=startInorder;
while(rootInorder<=endInorder&&*rootInorder!=rootValue) //在中序遍历中找到根节点
++rootInorder;
if(rootInorder==endInorder&&*rootInorder!=rootValue) //找到最后一点也不是要找的值则输入有误
throw exception("Invalid Input");
int leftLength=rootInorder-startInorder;
int* leftPreorderEnd=startPreorder+leftLength;
if(leftLength>0){
root->m_nLeft=ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
}
if(leftLenth<endPreorder-startPreorder)
root->m_nRight=ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
}
}
int m_nValue;
BinaryTreeNode* m_nLeft;
BinaryTreeNode* m_nRight;
};
BinaryTreeNode* 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);
}
BinaryTreeNode* ConstructCore(int* startPreorder,int* endPreorder,int* startInorder,int* endInorder){
int rootValue=startPreorder[0]; //第一个值为根节点
BinaryTreeNode* root=new BinaryTreeNode();
root->m_nValue=rootValue;
root->m_nLeft=root->m_nRight=NULL; //构建节点
if(startPreorder=endPreorder){
if(startInorder=endInorder&&*startPreorder=*startInorder) //只有一个结点的时候
return root;
else
throw exception("Invalid Input");
}
int* rootInorder=startInorder;
while(rootInorder<=endInorder&&*rootInorder!=rootValue) //在中序遍历中找到根节点
++rootInorder;
if(rootInorder==endInorder&&*rootInorder!=rootValue) //找到最后一点也不是要找的值则输入有误
throw exception("Invalid Input");
int leftLength=rootInorder-startInorder;
int* leftPreorderEnd=startPreorder+leftLength;
if(leftLength>0){
root->m_nLeft=ConstructCore(startPreorder+1,leftPreorderEnd,startInorder,rootInorder-1);
}
if(leftLenth<endPreorder-startPreorder)
root->m_nRight=ConstructCore(leftPreorderEnd+1,endPreorder,rootInorder+1,endInorder);
}
}
相关文章推荐
- 根据二叉树的前序遍历和中序遍历的结果,重建二叉树
- 根据前序遍历和中序遍历结果重建二叉树(递归方法)
- 根据二叉树的前序遍历和中序遍历结果重建二叉树
- 根据二叉树的前序遍历和中序遍历的结果重建出该二叉树
- 根据前序遍历和中序遍历结果重建二叉树(递归方法)
- 根据二叉树中序和后序(先序)遍历结果 重建二叉树
- 根据前序和中序遍历结果重建二叉树
- 根据二叉树的前序遍历和中序遍历重建二叉树
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
- 如何根据前序遍历和中序遍历重建二叉树
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5
- 根据前序遍历和中序遍历重建二叉树
- 输入某二叉树的前序遍历和中序遍历的结果请重建出该二叉树。
- POJ 2255Tree Recovery 二叉树重建(根据前序遍历和中序遍历写出后序遍历)
- 输入某二叉树的前序遍历和中序遍历的结果,重建此二叉树。
- 根据前序遍历序列和中序遍历序列重建二叉树
- (剑指offer笔记)根据前序遍历和后序遍历重建二叉树
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5
- 已知二叉树的前序遍历和中序遍历的结果,重建二叉树
- 重建二叉树(根据前序和中序遍历结果)