Home > Back-end >  Ask, where the code is wrong
Ask, where the code is wrong

Time:09-25

Topic: known preorder traversal and in sequence traversal, return to the root node of the tree (code at compile time, but no output value of the root node)
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; iIf ([I]==b * a) I=now;//now is root node subscript, so left subtree nodes for now, the right sub tree node number for num - now - 1
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; system("pause");
}



CodePudding user response:

Preorder traversal of the first number is the root node of the tree

CodePudding 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