• 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树


    L2-006. 树的遍历

     1 #include <stdio.h> 
     2 #include <stdlib.h>
     3 #include <string.h>
     4 #include <stdbool.h>
     5 
     6 struct node
     7 {
     8     long left,right;
     9 }tree[10000];
    10 
    11 long a[31],b[31];
    12 
    13 void work(long l,long r,long p,long q,long fa,long cond)
    14 {
    15     long root=a[r],pos;
    16     for (pos=p;pos<=q;pos++)
    17         if (b[pos]==root)
    18             break;
    19     if (cond==1)
    20         tree[fa].left=root;
    21     else
    22         tree[fa].right=root;
    23     if (p<=pos-1)
    24         work(l,l+pos-1-p,p,pos-1,root,1);
    25     if (pos+1<=q)
    26         work(l+pos-p,r-1,pos+1,q,root,2);
    27 }
    28 
    29 int main()
    30 {
    31     long i,head,tail,n;
    32     long q[31];
    33     scanf("%ld",&n);
    34     for (i=1;i<=n;i++)
    35     {
    36         scanf("%ld",&a[i]);
    37         tree[a[i]].left=0;
    38         tree[a[i]].right=0;
    39     }
    40     for (i=1;i<=n;i++)
    41         scanf("%ld",&b[i]);
    42     work(1,n,1,n,0,0);
    43     
    44     head=0;
    45     tail=1;
    46     q[1]=a[n];
    47     while (head<tail)
    48     {
    49         head++;
    50         printf("%ld",q[head]);
    51         if (tree[q[head]].left!=0)
    52         {
    53             tail++;
    54             q[tail]=tree[q[head]].left;
    55         }
    56         if (tree[q[head]].right!=0)
    57         {
    58             tail++;
    59             q[tail]=tree[q[head]].right;
    60         }
    61         if (head!=tail)
    62             printf(" ");
    63     }
    64     return 0;
    65 }

    L2-011. 玩转二叉树

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 struct node
     5 {
     6     long left,right;
     7 }tree[301];
     8 
     9 long a[301],b[301],q[301];
    10 
    11 void work(long x,long y,long p,long q,long father,long cond)
    12 {
    13     long pos,count;
    14     if (father!=0)
    15     {
    16         if (cond==1)
    17             tree[father].left=b[p];
    18         else
    19             tree[father].right=b[p];
    20     }
    21     for (pos=x;pos<=y;pos++)
    22         if (a[pos]==b[p])
    23             break;
    24     count=pos-x;
    25     if (count>0)
    26         work(x,pos-1,p+1,p+count,b[p],1);
    27     if (y-pos>0)
    28         work(pos+1,y,p+count+1,q,b[p],2);
    29 }
    30 
    31 void change(long d)
    32 {
    33     if (tree[d].left==0 && tree[d].right==0)
    34         return ;
    35     long temp;
    36     temp=tree[d].left;
    37     tree[d].left=tree[d].right;
    38     tree[d].right=temp;
    39     change(tree[d].left);
    40     change(tree[d].right);
    41 }
    42 
    43 int main()
    44 {
    45     long n,i,head,tail;
    46     scanf("%ld",&n);
    47     for (i=1;i<=n;i++)
    48         scanf("%ld",&a[i]); //mid
    49     for (i=1;i<=n;i++)
    50         scanf("%ld",&b[i]);    //pre
    51     for (i=1;i<=n;i++)
    52     {
    53         tree[i].left=0;
    54         tree[i].right=0;
    55     }    
    56     work(1,n,1,n,0,0);
    57     change(b[1]);
    58     
    59     head=0; tail=1;
    60     q[1]=b[1];
    61     while (head<tail)
    62     {
    63         head++;
    64         printf("%ld",q[head]);
    65         if (tree[q[head]].left!=0)
    66         {
    67             tail++;
    68             q[tail]=tree[q[head]].left;
    69         }
    70         if (tree[q[head]].right!=0)
    71         {
    72             tail++;
    73             q[tail]=tree[q[head]].right;
    74         }
    75         if (head!=tail)
    76             printf(" "); //output不能多余的空格 
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    Linux下yum安装mysql
    centos下无法使用lsof命令"-bash: lsof: command not found"
    大数据与区块链的联系与区别
    java.lang.NullPointerException报错的几种情况
    算法概述
    区块链简史
    word导出失败问题
    [Python3网络爬虫开发实战] 1.9.5-Scrapyrt的安装
    [Python3网络爬虫开发实战] 1.9.3-Scrapyd-Client的安装
    [Python3网络爬虫开发实战] 1.9.2-Scrapyd的安装
  • 原文地址:https://www.cnblogs.com/cmyg/p/8574708.html
Copyright © 2020-2023  润新知