-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-12347.cpp
More file actions
45 lines (37 loc) · 760 Bytes
/
UVA-12347.cpp
File metadata and controls
45 lines (37 loc) · 760 Bytes
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
#include <stdio.h>
#include <stdlib.h>
typedef struct bst{
int val;
struct bst *left;
struct bst *right;
}bst;
bst * bst_insert(bst *root, int v){
if(root == NULL){
bst *n = (bst *) malloc(sizeof(bst));
n->val = v;
n->left = NULL;
n->right = NULL;
return n;
}else if(v < root-> val){
root->left = bst_insert(root->left, v);
return root;
}else{
root->right = bst_insert(root->right, v);
return root;
}
}
void post_order(bst *root){
if(root == NULL) return;
post_order(root->left);
post_order(root->right);
printf("%d\n", root->val);
}
int main(){
bst *root = NULL;
int temp;
while(scanf("%d", &temp) != EOF){
root = bst_insert(root, temp);
}
post_order(root);
return 0;
}