-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.cpp
62 lines (60 loc) · 928 Bytes
/
3.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
#include<iostream>
#include<cstring>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
void CreateBiTree(BiTree& T, char S[], int& i)
{////先序建立二叉树
if (S[i] == '0')
{
T = NULL;
i++;
}
else
{
T = new BiTNode;
T->data = S[i];
i++;
CreateBiTree(T->lchild, S, i);
CreateBiTree(T->rchild, S, i);
}
}
void ChangeRL(BiTree& T)
{//二叉树左右孩子的交换
if (T == NULL)
{
return;
}
BiTree temp = T->lchild;
T->lchild = T->rchild;
T->rchild = temp;
ChangeRL(T->lchild);
ChangeRL(T->rchild);
}
void PreOrderTraverse(BiTree T)
{//先序遍历
if (T)
{
cout << T->data;
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
int main()
{
char S[100];
while (cin >> S)
{
if (strcmp(S, "0") == 0) break;
int i = -1;
BiTree T;
CreateBiTree(T, S, ++i);
ChangeRL(T);
PreOrderTraverse(T);
cout << endl;
}
return 0;
}