fork download
  1. #include <stdio.h>
  2.  
  3. #define MY_MAX(a,b) (a) > (b) ? (a) : (b)
  4. typedef struct link
  5. {
  6. int acc;
  7. struct link *l, *r;
  8. } link_t;
  9. typedef struct link *linkp;
  10.  
  11. int search_max(linkp h, int max)
  12. {
  13. if (h == NULL)
  14. return max;
  15. if (h->acc > max)
  16. max = h->acc;
  17. max = MY_MAX(search_max(h->l, max), search_max(h->r, max));
  18. return max;
  19. }
  20. int main(void) {
  21. link_t root = { 123, 0, 0 };
  22. link_t l1 = { 2, 0, 0 };
  23. link_t l2 = { 3, 0, 0 };
  24.  
  25. link_t l11 = { 5, 0, 0 };
  26. link_t l12 = { 1, 0, 0 };
  27.  
  28. link_t l21 = { 9, 0, 0 };
  29. link_t l211 = { 13, 0, 0 };
  30. link_t l212 = { 0, 0, 0 };
  31.  
  32. link_t l2121 = { 110, 0, 0 };
  33.  
  34. root.l = &l1;
  35. root.r = &l2;
  36.  
  37. l1.l = &l11;
  38. l1.r = &l12;
  39.  
  40. l2.l = &l21;
  41.  
  42. l21.l = &l211;
  43. l21.r = &l212;
  44.  
  45. l212.l = &l2121;
  46.  
  47. printf("max [%d]", search_max(&root,0));
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
max [123]