Code:
# include
# include
# include
using namespace std;
Struct BinaryTreeNode
{int m_nvalue;
BinaryTreeNode * m_pLeft;
BinaryTreeNode * m_pRight;
};
BinaryTreeNode * Rebuild (int * preorder, inorder, int * int num)//num is the total number of nodes of the tree
A={int * preorder.
Int * b=inorder;
Int now=1;
BinaryTreeNode * Head=new BinaryTreeNode ();
The Head - & gt; m_nvalue=https://bbs.csdn.net/topics/* a;
The Head - & gt; M_pLeft=NULL;
The Head - & gt; M_pRight=NULL;
for(int i=0; i
If (now> 0) Head - & gt; M_pLeft=Rebuild (a, b, now);
If ((num - now - 1) & gt; 0) Head - & gt; M_pRight=Rebuild (a + now + 1, b + + 1, now num - now - 1);
Return the Head;
}
Void main ()
{int preorder []=,2,4,7,3,5,6,8 {1};
,7,2,1,5,3,8,6 int inorder []={4};
BinaryTreeNode * H=new BinaryTreeNode ();
H=Rebuild (preorder, inorder, 8).
Cout<(H - & gt; M_nvalue) & lt;
}
CodePudding user response:
Preorder traversal of the first number is the root node of the treeCodePudding user response:
Preorder traversal of the first node is the root node of the tree, because I work to test the root node have achieved success, other nodes can also be used to test, subject is mainly to the tree was built up, harm