SHARE
TWEET

Untitled

a guest Oct 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. struct TREE{
  4.     int num;
  5.     TREE *left, *right;
  6. };
  7.  
  8. TREE* root = NULL;
  9.  
  10. TREE* create(int n){
  11.     TREE *t = new TREE();
  12.     if(t == NULL){
  13.         t->num = n;
  14.         t->left = t->right = NULL;
  15.         return t;
  16.     }
  17. }
  18.  
  19. void add(int n, TREE* index){
  20.     if(index == NULL){
  21.         index = create(n);
  22.         if(root == NULL)
  23.             root = index;
  24.     }
  25.     if(index->num > n){
  26.         add(n, index->left);
  27.     }else{
  28.         add(n, index->right);
  29.     }
  30. }
  31.  
  32. void print(TREE* cur){
  33.     if(cur != NULL){
  34.         print(cur->left);
  35.         printf("%d", cur->num);
  36.         print(cur->right);
  37.     }
  38. }
  39.  
  40. int main(){
  41.     int n, num;
  42.     TREE* node;
  43.     scanf("%d", &n);
  44.     for(int i = 0; i < n; i++){
  45.         scanf("%d", &num);
  46.         add(num, node);
  47.     }
  48.     print(root);
  49.     return 0;
  50. }
RAW Paste Data
Top