Advertisement
LA77

Untitled

Dec 11th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int n, c;
  4. int arr[(1 << 20) + 5];
  5.  
  6. void dac(int left, int right)
  7. {
  8.     if(left > right)
  9.     {
  10.         return;
  11.     }
  12.  
  13.     int mid = (left + right) / 2;
  14.  
  15.     if(right == left)
  16.     {
  17.         printf("Leaf %d", arr[mid]);
  18.         return;
  19.     }
  20.  
  21.     printf("Tree (");
  22.     dac(left, mid - 1);
  23.     printf(") %d (", arr[mid]);
  24.     dac(mid + 1, right);
  25.     printf(")");
  26. }
  27. /*
  28.  
  29. 4
  30. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  31.  
  32. Tree (Tree (Tree (Leaf 1) 2 (Leaf 3)) 4 (Tree (Leaf 5) 6 (Leaf 7))) 8 (Tree (Tree (Leaf 9) 10 (Leaf 11)) 12 (Tree (Leaf 13) 14 (Leaf 15)))
  33.  
  34. */
  35.  
  36. int main()
  37. {
  38.     scanf("%d", &n);
  39.  
  40.     c = (1 << n) - 1;
  41.  
  42.     for(int i = 0; i < c; ++ i)
  43.     {
  44.         scanf("%d", &arr[i]);
  45.     }
  46.     dac(0, c - 1);
  47.     printf("\n");
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement