第五次练习赛解题报告及标程

  因为五一的缘故,也因为静态二叉树的部分没什么可创新的题目,也因为陈题足够好,也因为我们犯懒……因而这次把两周的练习放到一起,同时放出了11道题。其中绝大多数题如果理解到位,并不需要真正地建立一颗树出来,所以我们鼓励大家尝试各种方法解决每道题。

  A. n皇后

  唯一一道和二叉树无关的题,经典题,禁止打表,标准的搜索,考查递归的功力。网上代码足够多且大同小异,这里放出一个位运算版本的供参考。

#include<cstdio>
using namespace std;
int mask,ans;
void dfs(int row,int left,int right)
{
    if(row==mask)
        ++ans;
    else
    {
        int pos=mask&~(row|left|right);
        while(pos)
        {
            int p=pos&-pos;
            pos-=p;
            dfs(row|p,(left|p)<<1,(right|p)>>1);
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        mask=(1<<n)-1;
        ans=0;
        dfs(0,0,0);
        printf("%d\n",ans);
    }
}

  B. 烦人的括号

  水题,常规做法是根据广义表建树,再输出先序遍历。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=10005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
void PreOrder(BTNode *b)
{
    BTNode *St[MAXN],*p;
    int top=-1;
    St[++top]=b;
    while(top>-1)
    {
        p=St[top--];
        printf("%s",p->data);
        if(p->rchild)
            St[++top]=p->rchild;
        if(p->lchild)
            St[++top]=p->lchild;
    }
    putchar('\n');
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        BTNode *b;
        scanf("%s",str);
        CreateBTNode(b);
        PreOrder(b);
        DestroyBT(b);
    }
}

  当然研究下广义表和先序遍历的关系后会发现,只要把广义表的符号都去掉就行了。

#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
const int MAXN=10005;
char str[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int l=strlen(str);
        for(int i=0; i<l; ++i)
            if(isalpha(str[i]))
                putchar(str[i]);
        putchar('\n');
    }
}

  C. 二叉树之数组转换广义表

  书上应该有源代码,从根节点递归构造广义表输出即可。

#include<cstdio>
#include<cstdlib>
using namespace std;
const int MAXN=1005;
struct BTNode
{
    int data;
    BTNode *lchild,*rchild;
};
int num[MAXN];
BTNode* trans(int n,int i)
{
    if(i>n||num[i]==-1)
        return NULL;
    BTNode *b=(BTNode *)malloc(sizeof(BTNode));
    b->data=num[i];
    b->lchild=trans(n,i<<1);
    b->rchild=trans(n,i<<1|1);
    return b;
}
void DispBTNode(BTNode *b)
{
    if(b)
    {
        printf("%d",b->data);
        if(b->lchild||b->rchild)
        {
            putchar('(');
            DispBTNode(b->lchild);
            if(b->rchild)
                putchar(',');
            DispBTNode(b->rchild);
            putchar(')');
        }
    }
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; ++i)
            scanf("%d",&num[i]);
        BTNode *b=trans(n,1);
        DispBTNode(b);
        DestroyBT(b);
    }
}

  D. 二叉树的三围

  做法不少。有的人是层次遍历时,在每个节点中记录下其所在的层次,然后寻找节点数最多的层次。我的做法是在建树时就根据广义表来记录层次信息,省去了一次遍历。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=1005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN];
int cnt[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,level=0,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            ++level;
            k=1;
            break;
        case ')':
            --top;
            --level;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            ++cnt[level];
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(cnt,0,sizeof(cnt));
        scanf("%s",str);
        BTNode *b;
        CreateBTNode(b);
        int i=-1,cmax=0,idx;
        while(cnt[++i]>0)
            if(cnt[i]>cmax)
            {
                cmax=cnt[i];
                idx=i;
            }
        if(idx!=0&&idx!=i-1)
            printf("%d %d %d\n",cnt[idx-1],cnt[idx],cnt[idx+1]);
        else
            puts("Invalid tree!");
        DestroyBT(b);
    }
}

  此外也可以不建树,直接把广义表当字符串处理。事实上我们发现广义表的括号就记录了层次信息。

#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=1005;
char str[MAXN];
int cnt[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(cnt,0,sizeof(cnt));
        scanf("%s",str);
        int level=0,l=strlen(str);
        for(int i=0; str[i]!='\0'; ++i)
            switch(str[i])
            {
            case '(':
                ++level;
                break;
            case ')':
                --level;
                break;
            case ',':
                break;
            default:
                while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                    ++i;
                --i;
                ++cnt[level];
            }
        int cmax=0,idx;
        for(level=0; cnt[level]>0; ++level)
            if(cnt[level]>cmax)
            {
                cmax=cnt[level];
                idx=level;
            }
        if(idx!=0&&idx!=level-1)
            printf("%d %d %d\n",cnt[idx-1],cnt[idx],cnt[idx+1]);
        else
            puts("Invalid tree!");
    }
}

  E. 二叉树也玩俄罗斯方块

  其实就是从根节点开始,寻找第一个节点不满的层。建树然后层次遍历,利用二叉树的一点数学性质罢了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=10005;
struct BTNode
{
    int data;
    BTNode *lchild,*rchild;
};
int num[MAXN];
BTNode* trans(int n,int i)
{
    if(i>=n)
        return NULL;
    BTNode *b=(BTNode *)malloc(sizeof(BTNode));
    b->data=num[i];
    b->lchild=trans(n,i<<1);
    b->rchild=trans(n,i<<1|1);
    return b;
}
int LevelOrder(BTNode *b)
{
    BTNode *qu[MAXN],*p;
    int front,rear,cnt=1;
    front=rear=-1;
    qu[++rear]=b;
    while(front!=rear)
    {
        front=(front+1)%MAXN;
        p=qu[front];
        if(p->data==-1)
            break;
        ++cnt;
        if(p->lchild)
        {
            rear=(rear+1)%MAXN;
            qu[rear]=p->lchild;
        }
        if(p->rchild)
        {
            rear=(rear+1)%MAXN;
            qu[rear]=p->rchild;
        }
    }
    return cnt;
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int n;
    BTNode *b;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; ++i)
            scanf("%d",&num[i]);
        b=trans(n,1);
        int ans=-1;
        for(int cnt=LevelOrder(b); cnt>0; cnt>>=1)
            ++ans;
        printf("%d\n",ans);
        DestroyBT(b);
    }
}

  因为给出的是数组表示,第一个值为-1的节点所在层就是不满的层,所以可以不建树,直接遍历数组就可以了。

#include<cstdio>
using namespace std;
const int MAXN=1005;
int num[MAXN];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; ++i)
            scanf("%d",&num[i]);
        int cnt=n+1,ans;
        for(int i=1; cnt==n+1&&i<=n; ++i)
            if(num[i]==-1)
                cnt=i;
        for(ans=0; cnt>0; cnt>>=1)
            ++ans;
        printf("%d\n",ans-1);
    }
}

  F. BusyJay的爬网机器人

  水题,建树时记录节点的层次,然后先序遍历的过程中输出。注意节点不一定是单字符的,先序遍历写了递归和非递归两个版本。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=1005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
void PreOrder(BTNode *b,int l)
{
    if(!b)
        return;
    for(int i=0; i<(l<<1); ++i)
        putchar(' ');
    puts(b->data);
    PreOrder(b->lchild,l+1);
    PreOrder(b->rchild,l+1);
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        BTNode *b;
        CreateBTNode(b);
        PreOrder(b,0);
        DestroyBT(b);
    }
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=1005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
void PreOrder(BTNode *b)
{
    BTNode *St[MAXN],*p;
    int top=-1,level[MAXN],l=0;
    St[++top]=b;
    level[top]=l;
    while(top>-1)
    {
        p=St[top];
        l=level[top--];
        for(int i=0; i<(l<<1); ++i)
            putchar(' ');
        puts(p->data);
        if(p->rchild)
        {
            St[++top]=p->rchild;
            level[top]=l+1;
        }
        if(p->lchild)
        {
            St[++top]=p->lchild;
            level[top]=l+1;
        }
    }
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        BTNode *b;
        CreateBTNode(b);
        PreOrder(b);
        DestroyBT(b);
    }
}

  不建树也可以,把广义表当作普通的字符串去处理,无非是一个简单的排版操作。

#include<cstdio>
using namespace std;
const int MAXN=1005;
char str[MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int level=0,l=strlen(str);
        for(int i=0; i<l; ++i)
            switch(str[i])
            {
            case '(':
                ++level;
                break;
            case ')':
                --level;
                break;
            case ',':
                break;
            default:
                for(int j=0; j<2*level; ++j)
                    putchar(' ');
                while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                    putchar(str[i++]);
                --i;
                putchar('\n');
            }
    }
}

  G. α星的等级体系(二)

  这个系列的题总共有六道,我们选了其中认为比较好的三道。这个就是给出广义表,判断是否是完全二叉树。只要理解好完全二叉树和满二叉树的区别就不难,无非判断两点,一是除最后一层外是否都是满的,二是最后一层的节点是否是编号连续的。当然这样做显得笨拙了一些,只要把广义表变成二叉树的数组表示,然后遍历一遍数组就好了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=30005;
struct BTNode
{
    char data;
    BTNode *lchild,*rchild;
};
char str[MAXN],SqBTree[MAXN<<1];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            p->data=str[i];
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
void PreOrder(BTNode *b,int i)
{
    if(!b)
        return;
    SqBTree[i]=b->data;
    PreOrder(b->lchild,i<<1);
    PreOrder(b->rchild,i<<1|1);
}
int BTNodeHeight(BTNode *b)
{
    int lchildh,rchildh;
    if(!b)
        return 0;
    lchildh=BTNodeHeight(b->lchild);
    rchildh=BTNodeHeight(b->rchild);
    return max(lchildh,rchildh)+1;
}
bool isCBT(BTNode *b)
{
    int cnt=1,h=BTNodeHeight(b);
    while(--h)
        cnt<<=1;
    for(int i=1; i<cnt; ++i)
        if(SqBTree[i]=='#')
            return false;
    for(int i=cnt; i<(cnt<<1); ++i)
        if(SqBTree[i]=='#')
            for(; i<(cnt<<1); ++i)
                if(SqBTree[i]!='#')
                    return false;
    return true;
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        memset(SqBTree,'#',sizeof(SqBTree));
        BTNode *b;
        CreateBTNode(b);
        PreOrder(b,1);
        puts(isCBT(b)?"YES":"NO");
        DestroyBT(b);
    }
}

  H. α星的等级体系(三)

  计算叶子节点个数,非常基础的题目。建树然后递归查找叶子节点就可以了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=30005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
int CalcLeaf(BTNode *b)
{
    if(!b)
        return 0;
    if(b->lchild||b->rchild)
        return CalcLeaf(b->lchild)+CalcLeaf(b->rchild);
    return 1;
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",str);
        BTNode *b;
        CreateBTNode(b);
        printf("%d\n",CalcLeaf(b));
        DestroyBT(b);
    }
}

  也可以抓住广义表中叶子节点的特征,不建树直接判断。

#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=30005;
char str[MAXN];
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",str);
        int l=strlen(str),ans=0;
        for(int i=0; i<l; ++i)
            switch(str[i])
            {
            case '(':
            case ')':
            case ',':
                break;
            default:
                while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                    ++i;
                if(str[i]!='('||(str[i]=='('&&str[i+1]==','&&str[i+2]==')'))
                    ++ans;
                --i;
            }
        printf("%d\n",ans);
    }
}

  I. α星的等级体系(六)

  在这次的题目中算是稍难的题,虽然考查的操作也很常规:找出二叉树中两个节点的最近公共祖先。从两个节点开始向根节点回溯,所以需要在节点上增加一个指向父节点的指针,且需要记录层次信息。这些工作都可以在建树时完成。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=50005;
struct BTNode
{
    char data[15];
    int level;
    BTNode *parent,*lchild,*rchild;
};
char str[MAXN];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,level=1,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            ++level;
            k=1;
            break;
        case ')':
            --top;
            --level;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->level=level;
            p->parent=p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
            {
                p->parent=St[top];
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
            }
        }
}
BTNode* FindNode(BTNode *b,char *s)
{
    BTNode *St[MAXN],*p,*h;
    int top=-1;
    St[++top]=b;
    while(top>-1)
    {
        p=St[top--];
        if(strcmp(s,p->data)==0)
            return p;
        if(p->rchild)
            St[++top]=p->rchild;
        if(p->lchild)
            St[++top]=p->lchild;
    }
    return NULL;
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        char an[15],bn[15];
        BTNode *t,*a,*b;
        bool flag=false;
        scanf("%s%s%s",str,an,bn);
        CreateBTNode(t);
        a=FindNode(t,an);
        b=FindNode(t,bn);
        if(a&&b)
        {
            while(strcmp(a->data,b->data)!=0)
                a->level>b->level?a=a->parent:b=b->parent;
            puts(a->data);
        }
        else
            puts("Sorry,They have not the same ancestor!");
        DestroyBT(t);
    }
}

  J. 在河之洲

  建树,遍历(怎么遍历都行)找到相应节点,记录层次距离;不难。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=1005;
struct BTNode
{
    char data[10];
    BTNode *lchild,*rchild;
};
char str[MAXN],k[10];
void CreateBTNode(BTNode *&b)
{
    BTNode *St[MAXN],*p;
    int top=-1,k,l=strlen(str);
    b=NULL;
    for(int i=0; i<l; ++i)
        switch(str[i])
        {
        case '(':
            St[++top]=p;
            k=1;
            break;
        case ')':
            --top;
            break;
        case ',':
            k=2;
            break;
        default:
            p=(BTNode *)malloc(sizeof(BTNode));
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                p->data[j++]=str[i++];
            --i;
            p->data[j]='\0';
            p->lchild=p->rchild=NULL;
            if(!b)
                b=p;
            else
                switch(k)
                {
                case 1:
                    St[top]->lchild=p;
                    break;
                case 2:
                    St[top]->rchild=p;
                    break;
                }
        }
}
int PreOrder(BTNode *b)
{
    BTNode *St[MAXN],*p;
    int top=-1,level[MAXN],l=0;
    St[++top]=b;
    level[top]=l;
    while(top>-1)
    {
        p=St[top];
        l=level[top--];
        if(strcmp(p->data,k)==0)
            return l;
        if(p->rchild)
        {
            St[++top]=p->rchild;
            level[top]=l+1;
        }
        if(p->lchild)
        {
            St[++top]=p->lchild;
            level[top]=l+1;
        }
    }
    return -1;
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%s%s",&n,k,str);
        BTNode *b;
        CreateBTNode(b);
        int l=PreOrder(b);
        puts(l!=-1&&n>=2*l?"Yes":"No");
        DestroyBT(b);
    }
}

  一样的可以不建树去做。在给出广义表的情况下,只与层次相关的题目都可以考虑不建树,前提是你能想清楚。

#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=1005;
char str[MAXN],k[10];
int LookForAnn()
{
    int level=0,l=strlen(str);
    char data[10];
    for(int i=0; i<level; ++i)
        switch(str[i])
        {
        case '(':
            ++level;
            break;
        case ')':
            --level;
            break;
        case ',':
            break;
        default:
            int j=0;
            while(i<l&&str[i]!='('&&str[i]!=')'&&str[i]!=',')
                data[j++]=str[i++];
            --i;
            data[j]='\0';
            if(strcmp(data,k)==0)
                return level;
        }
    return -1;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%s%s",&n,k,str);
        int level=LookForAnn();
        puts(level!=-1&&n>=2*level?"Yes":"No");
    }
}

  K. 遍历构造二叉树

  给出先序和中序遍历,得到后序遍历。书上有通过先序和中序遍历建树的代码,然后再后序遍历一次就可以了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=105;
struct BTNode
{
    char data;
    BTNode *lchild,*rchild;
};
BTNode* CreateBT(char *pre,char *in,int n)
{
    if(n<=0)
        return NULL;
    BTNode *b=(BTNode *)malloc(sizeof(BTNode));
    b->data=*pre;
    char *p;
    for(p=in; p<in+n; ++p)
        if(*p==*pre)
            break;
    int k=p-in;
    b->lchild=CreateBT(pre+1,in,k);
    b->rchild=CreateBT(pre+k+1,p+1,n-k-1);
    return b;
}
void PostOrder(BTNode *b)
{
    BTNode *St[MAXN],*p;
    int top=-1;
    bool flag;
    do
    {
        while(b)
        {
            St[++top]=b;
            b=b->lchild;
        }
        p=NULL;
        flag=true;
        while(top>-1&&flag)
        {
            b=St[top];
            if(b->rchild==p)
            {
                putchar(b->data);
                --top;
                p=b;
            }
            else
            {
                b=b->rchild;
                flag=false;
            }
        }
    }
    while(top>-1);
    putchar('\n');
}
void DestroyBT(BTNode *&b)
{
    if(b->lchild)
        DestroyBT(b->lchild);
    if(b->rchild)
        DestroyBT(b->rchild);
    free(b);
}
int main()
{
    int n;
    char a[MAXN],b[MAXN];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s",a,b);
        BTNode *t=CreateBT(a,b,strlen(a));
        PostOrder(t);
        DestroyBT(t);
    }
}

  当然也可以不建树,变成了彻头彻尾的递归题。

#include<cstdio>
#include<cstring>
const int MAXN=105;
char pres[MAXN],ins[MAXN],posts[MAXN];
void trans(int l,char* s1,char* s2,char* s3)
{
    if(l<=0)
        return;
    int pos=strchr(s2,s1[0])-s2;
    trans(pos,s1+1,s2,s3);
    trans(l-pos-1,s1+pos+1,s2+pos+1,s3+pos);
    s3[l-1]=s1[0];
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s%s",pres,ins);
        int l=strlen(pres);
        trans(l,pres,ins,posts);
        posts[l]='\0';
        puts(posts);
    }
}

  总得来说这次的题目相当简单,而且考查了较多的基础知识,如果能够独立AK,说明静态二叉树的相关知识已经没什么问题了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值