-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1136.cpp
More file actions
61 lines (59 loc) · 1.23 KB
/
1136.cpp
File metadata and controls
61 lines (59 loc) · 1.23 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
60
61
#include <iostream>
#include <iomanip>
using namespace std;
int sk[3000];
int l = -1;
struct tnode {
int field;
struct tnode* left;
struct tnode* right;
};
void treeprint(tnode* tree) {
if (tree != NULL) {
l++;
sk[l] = tree->field;
treeprint(tree->left);
treeprint(tree->right);
}
}
struct tnode* addnode(int x, tnode* tree) {
if (tree == NULL) {
tree = new tnode;
tree->field = x;
tree->left = NULL;
tree->right = NULL;
}
else if (x < tree->field)
tree->left = addnode(x, tree->left);
else
tree->right = addnode(x, tree->right);
return(tree);
}
void freemem(tnode* tree) {
if (tree != NULL) {
freemem(tree->left);
freemem(tree->right);
delete tree;
}
}
int main()
{
struct tnode* root = 0;
int a,r;
cin >> r;
int* fr = new int[r];
for (int i = 0; i < r; i++)
cin >> fr[i];
a = fr[r - 1];
root = addnode(a, root);
for (int i = r-2; i >=0; i--)
{
a=fr[i];
root = addnode(a, root);
}
treeprint(root);
freemem(root);
for (int i = r - 1; i >= 0; i--)
cout << sk[i] << endl;
return 0;
}