Idea: first create the root node based on the first number of first sequence, then locate the root node position in the middle sequence, and then determine the pre-sequence and post-sequence of the left and right subtree, recursively construct left and right subtree.
C ++ code:
# Include "stdafx. h "# include <iostream> # include <assert. h> using namespace std; struct BiTreeNode {int m_nData; BiTreeNode * m_pLeftChild; BiTreeNode * m_pRightChild;}; BiTreeNode * substring (int * preOrder, int nPreStart, int nPreEnd, int * inOrder, int nInStart, int nInEnd) {// exit if (nPreStart> nPreEnd) {return NULL;} // locate the root node int nRootDate = preOrder [nPreStart] According to the first sequence; // In Root Node int nCount = 0; int nCur = 0; for (nCur = nInStart; nCur <= nInEnd; nCur ++) {if (nRootDate! = InOrder [nCur]) {nCount ++; // nCount records the number of nodes in the left subtree} else {break ;}} assert (nCur> = nInStart & nCur <= nInEnd); // create the node BiTreeNode * pRoot = new BiTreeNode; pRoot-> m_nData = nRootDate; // according to the ordinal sequence, divide two sequences and perform recursive processing. PRoot-> m_pLeftChild = values (preOrder, nPreStart + 1, nPreStart + nCount, inOrder, nInStart, nInStart + nCount-1); pRoot-> m_pRightChild = CreateBiTreeByPreorderAndInorder (preOrder, nPreStart + nCount + 1, nPreEnd, inOrder, nInStart + nCount + 1, nInEnd); return pRoot ;} // re-build the binary tree BiTreeNode * CreateBiTreeByPreorderAndInorder (int * preOrder, int * inOrder, int nLe Ngth) {if (preOrder! = NULL) & (inOrder! = NULL) & (nLength> 0) {return CreateBiTreeByPreorderAndInorder (preOrder, 0, nLength-1, inOrder, 0, nLength-1);} else {return NULL ;}} void PreOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {cout <pRoot-> m_nData <""; PreOrderPrint (pRoot-> m_pLeftChild); PreOrderPrint (pRoot-> m_pRightChild );}} void InOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {InOrderPrint (pRoot-> m_pLeftChild); cout <pRoot-> m_nData <""; InOrderPrint (pRoot-> m_pRightChild );}} int _ tmain (int argc, _ TCHAR * argv []) {int nPreOrderArr [8] = {,}; int nInOrderArr [8] =, 2, 1, 5, 3, 8, 6}; BiTreeNode * pRoot = CreateBiTreeByPreorderAndInorder (nPreOrderArr, nInOrderArr, 8); cout <"FIFO sequence:"; PreOrderPrint (pRoot); cout <; cout <"post sequence:"; InOrderPrint (PRoot); cout <endl; system ("pause"); return 0 ;}# include "stdafx. h "# include <iostream> # include <assert. h> using namespace std; struct BiTreeNode {int m_nData; BiTreeNode * m_pLeftChild; BiTreeNode * m_pRightChild;}; BiTreeNode * substring (int * preOrder, int nPreStart, int nPreEnd, int * inOrder, int nInStart, int nInEnd) {// exit if (nPreStart> nPreEnd) {return NULL;} // locate the root node int n according to the first sequence RootDate = preOrder [nPreStart]; // find the root node int nCount = 0 in the middle sequence; int nCur = 0; for (nCur = nInStart; nCur <= nInEnd; nCur ++) {if (nRootDate! = InOrder [nCur]) {nCount ++; // nCount records the number of nodes in the left subtree} else {break ;}} assert (nCur> = nInStart & nCur <= nInEnd); // create the node BiTreeNode * pRoot = new BiTreeNode; pRoot-> m_nData = nRootDate; // according to the ordinal sequence, divide two sequences and perform recursive processing. PRoot-> m_pLeftChild = values (preOrder, nPreStart + 1, nPreStart + nCount, inOrder, nInStart, nInStart + nCount-1); pRoot-> m_pRightChild = CreateBiTreeByPreorderAndInorder (preOrder, nPreStart + nCount + 1, nPreEnd, inOrder, nInStart + nCount + 1, nInEnd); return pRoot ;} // rebuild the binary tree BiTreeNode * CreateBiTreeByPreorderAndInorder (int * preOrder, int * inOrder, int nLength ){ If (preOrder! = NULL) & (inOrder! = NULL) & (nLength> 0) {return CreateBiTreeByPreorderAndInorder (preOrder, 0, nLength-1, inOrder, 0, nLength-1);} else {return NULL ;}} void PreOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {cout <pRoot-> m_nData <""; PreOrderPrint (pRoot-> m_pLeftChild); PreOrderPrint (pRoot-> m_pRightChild );}} void InOrderPrint (BiTreeNode * pRoot) {if (pRoot! = NULL) {InOrderPrint (pRoot-> m_pLeftChild); cout <pRoot-> m_nData <""; InOrderPrint (pRoot-> m_pRightChild );}} int _ tmain (int argc, _ TCHAR * argv []) {int nPreOrderArr [8] = {,}; int nInOrderArr [8] =, 2, 1, 5, 3, 8, 6}; BiTreeNode * pRoot = CreateBiTreeByPreorderAndInorder (nPreOrderArr, nInOrderArr, 8); cout <"FIFO sequence:"; PreOrderPrint (pRoot); cout <; cout <"post sequence:"; InOrderPrint (pRoot); cout <endl; system ("pause"); return 0 ;}