fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. typedef struct tree{
  6. int key;
  7. struct tree *left;
  8. struct tree *right;
  9. } tree;
  10.  
  11. tree * insert(tree *root, int new_value){
  12. if(root == NULL){
  13. root = (tree *)malloc(sizeof(tree));
  14. root->key = new_value;
  15. root->left = root->right = NULL;
  16. return root;
  17. }
  18. if(root->key > new_value)
  19. root->left = insert(root->left, new_value);
  20. else
  21. root->right = insert(root->right, new_value);
  22. }
  23.  
  24. void postorder(tree *root){
  25. if(root == NULL)
  26. return;
  27. postorder(root->left);
  28. postorder(root->right);
  29. printf("%d ", root->key);
  30. }
  31.  
  32. int main(){
  33. int i, k;
  34. tree *root = NULL;
  35.  
  36. srand(time(NULL));
  37. for(i = 0; i < 10; i++){
  38. k = rand() % 17;
  39. root = insert(root, k);
  40. }
  41.  
  42. postorder(root);
  43.  
  44. scanf("%d", &i);
  45. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
6