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
|
typedef struct Node {
int data;
struct Node* left;
struct Node* right;
}Node;
void insertNode(Node* node, int x) {
Node* parent;
Node* tmp;
Node* newNode;
parent = NULL;
tmp = node;
while (tmp != NULL) {
parent = tmp;
//삽입하기 적절한 위치로 이동한다.
if (tmp->data > x) {
//왼쪽자식=
if (tmp->left != NULL) {
tmp = tmp->left;
}
else break;
}
else {
if (tmp->right != NULL) {
tmp = tmp->right;
}
else {
break;
}
}
}
newNode = new Node;
if (parent->data > x) {
parent->left = newNode;
}
else
parent->right = newNode;
newNode->data = x;
newNode->left = NULL;
newNode->right = NULL;
}
void postOrder(Node* root) {
if (root) {
postOrder(root->left);
postOrder(root->right);
cout << root->data << ' ';
}
}
int main() {
// insert code here...
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tmp;
cin >> tmp;
int root_data;
root_data = tmp;
Node* root = new Node;
root->data = root_data;
root->left = NULL;
root->right = NULL;
while (cin>>tmp) {
insertNode(root,tmp);
}
postOrder(root);
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'Computer Science' 카테고리의 다른 글
올림, 내림, 반올림 함수 (0) | 2019.08.29 |
---|---|
c++ 문자열 변환 (0) | 2019.08.28 |
Binary Tree (0) | 2019.08.09 |
HashMap 정렬 (0) | 2019.05.03 |
Java에서 HexString 값을 int로 변경 (0) | 2019.05.03 |