fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node {
  5. int data;
  6. struct node *left;
  7. struct node *right;
  8. };
  9.  
  10. typedef struct node node;
  11.  
  12.  
  13. node* newNode(int data) {
  14. node* node = malloc(sizeof(node));
  15. node->data = data;
  16. node->left = NULL;
  17. node->right = NULL;
  18. return node;
  19. }
  20.  
  21. void ImprimePreOrdemAux(node* arv, int nivel) {
  22. if(arv!=NULL) {
  23. if (nivel != 0) {
  24. printf("-");
  25. }
  26. printf("%d",arv->data);
  27. ImprimePreOrdemAux(arv->left, nivel + 1);
  28. ImprimePreOrdemAux(arv->right, nivel + 1);
  29. }
  30. }
  31.  
  32. void ImprimePreOrdem(node* arv) {
  33. ImprimePreOrdemAux(arv, 0);
  34. }
  35.  
  36. int main(){
  37. node *root = newNode(1);
  38. root->left = newNode(2);
  39. root->right = newNode(3);
  40. root->left->left = newNode(4);
  41.  
  42. ImprimePreOrdem(root);
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
1-2-4-3