-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_ConstructBinaryTree.cpp
More file actions
59 lines (52 loc) · 1.33 KB
/
Copy path6_ConstructBinaryTree.cpp
File metadata and controls
59 lines (52 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* ConstructBinaryTree(int preorder[], int inorder[], int len)
{
BinaryTreeNode* head = NULL;
if (preorder != NULL && inorder != NULL && len > 0)
{
BinaryTreeNode* pNew = new BinaryTreeNode();
pNew->m_nValue = preorder[0];
pNew->m_pLeft = NULL;
pNew->m_pRight = NULL;
head = pNew;
int i = 0;
for (; i < len; i++)
{
if (inorder[i] == preorder[0])
break;
}
if (i > 0)
head->m_pLeft = ConstructBinaryTree(preorder+1, inorder, i);
if (i < len-1)
head->m_pRight = ConstructBinaryTree(preorder + i + 1, inorder + i + 1,len- i - 1);
}
return head;
}
void print(BinaryTreeNode* head)
{
if (head != NULL)
{
cout << head->m_nValue << " ";
cout << "{" << " ";
print(head->m_pLeft);
cout << "}" << " ";
cout << "{" << " ";
print(head->m_pRight);
cout << "}" << " ";
}
}
int main()
{
int preorder[] = {1, 2, 4, 7, 3, 5, 6, 8};
int inorder[] = {4, 7, 2, 1, 5, 3, 8, 6};
BinaryTreeNode* head = ConstructBinaryTree(preorder, inorder, 8);
print(head);
return 0;
}