Advertisement
Emiliatan

d526

Apr 5th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. /* d526             */
  2. /* AC (33ms, 300KB) */
  3. #include <cstdio>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. typedef unsigned int uint;
  9.  
  10. typedef struct Node
  11. {
  12.     Node *left, *right;
  13.     uint data;
  14. }node;
  15.  
  16. Node *addnode(node *now, const uint &value)
  17. {
  18.     if(now == NULL)
  19.     {
  20.         now = (Node *)malloc(sizeof(struct Node));
  21.         now -> left = now -> right = NULL;
  22.         now -> data = value;
  23.     }
  24.     else if(now -> data > value) now -> left = addnode(now -> left, value);
  25.     else now -> right = addnode(now -> right, value);
  26.     return now;
  27. }
  28.  
  29. void show(node *now)
  30. {
  31.     if(now == NULL) return;
  32.     printf("%u ", now -> data);
  33.     show(now -> left), show(now -> right);
  34. }
  35.  
  36. void del(node *now)
  37. {
  38.     if(now == NULL) return;
  39.     del(now -> left), del(now -> right);
  40.     delete now;
  41. }
  42.  
  43. int main()
  44. {
  45.     uint n,m;
  46.     node *root = NULL;
  47.  
  48.     while(~scanf("%u", &n))
  49.     {
  50.         root = NULL;
  51.  
  52.         for(int i = 0; i < n && scanf("%u", &m); i++)
  53.             root = addnode(root,m);
  54.  
  55.         show(root);
  56.         del(root);
  57.  
  58.         puts("");
  59.     }
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement