【二叉树】遍历,叶子数,深度问题

1.SDUT 3341 遍历二叉树

/*
Problem Description
已知二叉树的先序遍历字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
每组输入数据对应输出2行:第1行输出中序遍历序列;第2行输出后序遍历序列。
Sample Input
abc,,de,g,,f,,,
Sample Output
cbegdfa
cgefdba 

*/
#include <stdio.h>
#include <stdlib.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
}node;
int l;
node *creat(char a[])
{
    char c;
    node *root;
    if(!a[l])
        return NULL;
    root=(node*)malloc(sizeof(node));
    c=a[l++];
    if(c==',')return NULL;
    root->data=c;
    root->lc=creat(a);
    root->rc=creat(a);
    return root;
}
void mid(node *root)
{
    if(root)
    {
        mid(root->lc);
        printf("%c",root->data);
        mid(root->rc);
    }
 
}
void after(node *root)
{
    if(root)
    {
        after(root->lc);
        after(root->rc);
        printf("%c",root->data);
    }
}
int main()
{
    char a[60];
    node *root;
    while(~scanf("%s",a))
    {
        l=0;
        root=creat(a);
 
        mid(root);
        printf("\n");
 
        after(root);
        printf("\n");
    }
    return 0;
}

2.SDUT 3342 数据结构实验之二叉树三:统计叶子数

/*
Problem Description
已知二叉树先序遍历的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并求二叉树的叶子结点个数。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
输出二叉树的叶子结点个数。
Sample Input
abc,,de,g,,f,,,
Sample Output
3
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
} node;
int l;
node *creat(char a[])
{
    char c;
    node *root;
    if(!a[l])
        return NULL;
    root=(node*)malloc(sizeof(node));
    c=a[l++];
    if(c==',')return NULL;
    root->data=c;
    root->lc=creat(a);
    root->rc=creat(a);
    return root;
}
int cnt;
void ye(node *root)
{
    if(root)
    {
        if(!root->lc&&!root->rc)
            cnt++;
        else
        {
            ye(root->lc);
            ye(root->rc);
        }
    }
}
int main()
{
    char a[60];
    node *root;
    while(~scanf("%s",a))
    {
        l=0;
        root=creat(a);
        cnt=0;
        ye(root);
        printf("%d\n",cnt);
    }
    return 0;
}

3.数据结构实验之二叉树八:(中序后序)求二叉树的深度

/*
Problem Description
已知一颗二叉树的中序遍历序列和后序遍历序列,求二叉树的深度。
Input
输入数据有多组,输入T,代表有T组数据。每组数据包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历,第二个表示二叉树的后序遍历。
Output
输出二叉树的深度。
Sample Input
2
dbgeafc
dgebfca
lnixu
linux
Sample Output
4
3
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct no
{
    int data;
    struct no *lc,*rc;
}node;
int max;
node *creat(int len,char mid[],char end[])
{
    node *root;
    int i;
    if(len==0)
        return NULL;
    for(i=0;i<len;i++)
    {
        if(end[len-1]==mid[i])
            break;
    }
    root=(node *)malloc(sizeof(node));
    root->data=mid[i];
    root->lc=creat(i,mid,end);
    root->rc=creat(len-i-1,mid+i+1,end+i);
    return root;
}
void deep(node *root,int c)
{
    if(root)
    {
       deep(root->lc,c+1);
        deep(root->rc,c+1);
        if(c>max)max=c;
    }
}
int main()
{
    int t;
    node *root;
    char mid[60],end[60];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s %s",mid,end);
        root=creat(strlen(mid),mid,end);
        max=0;
        deep(root,1);
        printf("%d\n",max);
    }
    return 0;
}

4.数据结构实验之二叉树七:叶子问题

/*
Problem Description
已知一个按先序输入的字符序列,如abd,,eg,,,cf,,,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。
Input
 输入数据有多行,每一行是一个长度小于50个字符的字符串。
Output
 按从上到下从左到右的顺序输出二叉树的叶子结点。
Sample Input
abd,,eg,,,cf,,,
xnl,,i,,u,,
Sample Output
dfg
uli
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
} node;
int l;
node *creat(char a[])
{
    char c=a[l++];
    node *root;
    if(c==',')
        return NULL;
    root=(node *)malloc(sizeof(node));
    root->data=c;
    root->lc=creat(a);
    root->rc=creat(a);
    return root;
}
void ceng(node *root)
{
    node *que[1003];
    int in=0,out=0;
    que[in++]=root;
    while(in>out)
    {
        if(que[out])
        {
            if(!que[out]->lc&&!que[out]->rc)
                printf("%c",que[out]->data);
            else
            {
                que[in++]=que[out]->lc;
                que[in++]=que[out]->rc;
            }
        }
        out++;
    }
}
int main()
{
    int t;
    char a[1003];
    node *root;
    while(~scanf("%s",a))
    {
        l=0;
        root=creat(a);
        ceng(root);
        printf("\n");
    }
    return 0;
}

5.数据结构实验之二叉树四:(先序中序)还原二叉树

/*
Problem Description
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
Input
输入数据有多组,每组数据第一行输入1个正整数N(1 <= N <= 50)为树中结点总数,随后2行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区分大小写)的字符串。
Output
 输出一个整数,即该二叉树的高度。
Sample Input
9 
ABDFGHIEC
FDHGIBEAC
Sample Output
5

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct no
{
    char data;
    struct no *lc,*rc;
} node;
int max;
 
node *creat(int len,char fon[],char mid[])
{
    node *root;
    int i;
    char c=fon[0];
    root=(node *)malloc(sizeof(node));
    root->lc=root->rc=NULL;
    for(i=0; i<len; i++)
    {
        if(c==mid[i])
        {
            root->data=c;
            root->lc=creat(i,fon+1,mid);
            root->rc=creat(len-1-i,fon+i+1,mid+i+1);
            break;
        }
    }
 
    return root;
}
void deep(node *root,int c)
{
    if(root)
    {
        deep(root->lc,c+1);
        deep(root->rc,c+1);
        if(c>max)max=c;
    }
}
int main()
{
    int n,len;
    char fon[90],mid[90];
    node *root;
    while(~scanf("%d",&len))
    {
        scanf("%s %s",fon,mid);
        root=creat(len,fon,mid);
        max=0;
        deep(root,0);
        printf("%d\n",max);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值