Advertisement
wojiaocbj

Untitled

Aug 8th, 2022
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #pragma warning(disable:4996)
  7. typedef long long s64, LL;
  8. typedef unsigned long long u64, ULL;
  9. typedef struct treenode{
  10.     int l, r, data;
  11. }treenode;
  12. treenode nodes[114514] = { 0 };
  13. void dfs1(int root){
  14.     if(nodes[root].data){
  15.         printf("%d ", nodes[root].data);
  16.         if(nodes[root].l){
  17.             dfs1(nodes[root].l);
  18.         }
  19.         if(nodes[root].r){
  20.             dfs1(nodes[root].r);
  21.         }
  22.     }
  23.     else{
  24.         return;
  25.     }
  26. }
  27. void dfs2(int root){
  28.     if(nodes[root].data){
  29.         if(nodes[root].l){
  30.             dfs2(nodes[root].l);
  31.         }
  32.         printf("%d ", nodes[root].data);
  33.         if(nodes[root].r){
  34.             dfs2(nodes[root].r);
  35.         }
  36.     }
  37.     else{
  38.         return;
  39.     }
  40. }
  41. void dfs3(int root){
  42.     if(nodes[root].data){
  43.         if(nodes[root].l){
  44.             dfs3(nodes[root].l);
  45.         }
  46.         if(nodes[root].r){
  47.             dfs3(nodes[root].r);
  48.         }
  49.         printf("%d ", nodes[root].data);
  50.     }
  51.     else{
  52.         return;
  53.     }
  54. }
  55. int main(){
  56.     int i, n;
  57.     scanf("%d", &n);
  58.     for(i = 1; i < (1 << (n - 1)); i++){
  59.         nodes[i].l = i << 1;
  60.         nodes[i].r = (i << 1) | 1;
  61.     }
  62.     for(i = 1; i < (1 << n); i++){
  63.         scanf("%d", &nodes[i].data);
  64.     }
  65.     dfs1(1);
  66.     putchar('\n');
  67.     dfs2(1);
  68.     putchar('\n');
  69.     dfs3(1);
  70.     putchar('\n');
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement