forked from pgxiaolianzi/microsoft-interview-100
/
weiruan_1.cpp
122 lines (106 loc) · 1.76 KB
/
weiruan_1.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
struct BSTreeNode
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
BSTreeNode* treeRoot = 0;
BSTreeNode* treeHead = 0;
BSTreeNode* pre = 0;
void convert(BSTreeNode* root)
{
if(root != 0)
{
convert(root->m_pLeft);
//printf("%d ", root->m_nValue);
if(pre)
{
pre->m_pRight= root;
root->m_pLeft = pre;
//printf("link\n");
}
if(!treeHead)
treeHead = root;
pre = root;
convert(root->m_pRight);
}
}
void DestroyLink(BSTreeNode* head)
{
while(head)
{
BSTreeNode* p = head->m_pRight;
delete head;
head = p;
}
}
void visitLink(BSTreeNode* head)
{
//printf("\n");
while(head)
{
printf("%d ", head->m_nValue);
head = head->m_pRight;
}
printf("\n");
}
void insert(int value)
{
if(treeRoot == 0)
{
treeRoot = new BSTreeNode();
treeRoot->m_nValue = value;
treeRoot->m_pLeft = 0;
treeRoot->m_pRight = 0;
//printf("tag\n");
}
else
{
BSTreeNode* p = treeRoot, *q;
BSTreeNode* newNode;
while(p != 0)
{
q = p;
if(p->m_nValue >= value)
p = p->m_pLeft;
else
p = p->m_pRight;
}
newNode = new BSTreeNode();
newNode->m_nValue = value;
newNode->m_pLeft = 0;
newNode->m_pRight = 0;
if(q->m_nValue >= value)
q->m_pLeft = newNode;
else
q->m_pRight = newNode;
}
//printf("tag\n");
}
void createTree(int* a, int n)
{
for(int i = 0; i < n; i++)
insert(a[i]);
//printf("tag\n");
}
void DestroyTree(BSTreeNode* root)
{
if(root)
{
DestroyTree(root->m_pLeft);
DestroyTree(root->m_pRight);
delete root;
root = 0;
}
}
int main()
{
int a[7] = {10, 6, 14, 4, 8, 12, 16};
createTree(a, 7);
convert(treeRoot);
treeRoot = treeHead;
visitLink(treeRoot);
DestroyLink(treeRoot);
//DestroyTree(root);
}