Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int n, c;
- int arr[(1 << 20) + 5];
- void dac(int left, int right)
- {
- if(left > right)
- {
- return;
- }
- int mid = (left + right) / 2;
- if(right == left)
- {
- printf("Leaf %d", arr[mid]);
- return;
- }
- printf("Tree (");
- dac(left, mid - 1);
- printf(") %d (", arr[mid]);
- dac(mid + 1, right);
- printf(")");
- }
- /*
- 4
- 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- 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)))
- */
- int main()
- {
- scanf("%d", &n);
- c = (1 << n) - 1;
- for(int i = 0; i < c; ++ i)
- {
- scanf("%d", &arr[i]);
- }
- dac(0, c - 1);
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement