Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct TREE{
- int num;
- TREE *left, *right;
- };
- TREE* root = NULL;
- TREE* create(int n){
- TREE *t = new TREE();
- if(t == NULL){
- t->num = n;
- t->left = t->right = NULL;
- return t;
- }
- }
- void add(int n, TREE* index){
- if(index == NULL){
- index = create(n);
- if(root == NULL)
- root = index;
- }
- if(index->num > n){
- add(n, index->left);
- }else{
- add(n, index->right);
- }
- }
- void print(TREE* cur){
- if(cur != NULL){
- print(cur->left);
- printf("%d", cur->num);
- print(cur->right);
- }
- }
- int main(){
- int n, num;
- TREE* node;
- scanf("%d", &n);
- for(int i = 0; i < n; i++){
- scanf("%d", &num);
- add(num, node);
- }
- print(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement