nyist oj 756 重建二叉树

38 篇文章 0 订阅
30 篇文章 0 订阅

重建二叉树

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!)。
输入
输入有多组数据(少于100组),以文件结尾结束。
每组数据仅一行,包括两个字符串,中间用空格隔开,分别表示二叉树的后序和中序序列(字符串长度小于26,输入数据保证合法)。
输出
每组输出数据单独占一行,输出对应得先序序列。
样例输入
ACBFGED ABCDEFG
CDAB CBAD
样例输出
DBACEGF
BCAD
来源
原创
上传者

TC_黄平


这道题主要考查对二叉树的遍历的熟悉程度,对先序遍历,中序遍历,后序遍历的掌握程度;

由后序遍历可以得到,最后一个字母应该就是树的根节点,中序遍历是先访问左子树,后访问根节点,在访问右子树,然后通过中序遍历的序列,可以把这颗树分成左右子树,得出这颗树的结构,然后再递归得出先序遍历的序列;

下面是代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
struct node
{
    char value;
    node *lchild,*rchild;//左孩子,右孩子
};
node *newnode(char c)
{
  node *p=(node *)malloc(sizeof(node));
  p->value=c;
  p->lchild=p->rchild=NULL;
  return p;
}
node *rebulid(char *post,char *in,int n)
{
    if(n==0) return NULL;
    char ch=post[n-1];//得到的是根节点的值
    node *p=newnode(ch);
    int i;
    for(i=0;i<n&&in[i]!=ch;i++);
    int l_len=i;
    int r_len=n-i-1;
    if(l_len>0) p->lchild=rebulid(post,in,l_len);//由中序遍历得出左右子树的值
    if(r_len>0) p->rchild=rebulid(post + l_len, in+l_len+1, r_len);
    return p;
}
void preorder(node *p)//先序遍历
{
    if(p==NULL) return;
    printf("%c",p->value);
    preorder(p->lchild);
    preorder(p->rchild);
}
int main()
{
    char postorder[30],inorder[30];
    while(scanf("%s%s",postorder,inorder)!=EOF)
    {
        node *root=rebulid(postorder,inorder,strlen(postorder));
        preorder(root);
        printf("\n");
    }
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是51单片机控制的12864液晶显示系统编程实现所需的代码: ``` #include <reg51.h> #include <intrins.h> #define LCD_DB P0 sbit LCD_RS = P2^6; // RS信号 sbit LCD_RW = P2^5; // RW信号 sbit LCD_E = P2^7; // E信号 sbit LCD_CS1 = P2^0; // CS1信号 sbit LCD_CS2 = P2^1; // CS2信号 void delay(unsigned int i) // 延时函数 { while(i--); } void write_com(unsigned char com) // 写命令函数 { LCD_RS = 0; LCD_RW = 0; LCD_DB = com; LCD_E = 1; _nop_(); _nop_(); LCD_E = 0; } void write_data(unsigned char dat) // 写数据函数 { LCD_RS = 1; LCD_RW = 0; LCD_DB = dat; LCD_E = 1; _nop_(); _nop_(); LCD_E = 0; } void init_lcd() // 初始化函数 { write_com(0xc0); // 设置显示模式 write_com(0x3f); // 允许显示 write_com(0x40); // 设置起始地址 write_com(0xb8); // 设置页地址 write_com(0xc0); // 设置列地址 write_com(0x40); // 设置起始地址 write_com(0xb8); // 设置页地址 write_com(0xc0); // 设置列地址 } void display_string(unsigned char x, unsigned char y, unsigned char *s) // 显示字符串函数 { unsigned char i; if(y == 0) // 第一行居中显示“南阳理工学院” { write_com(0xb8 + x); write_com(0x40); } else if(y == 1) // 第二行居中显示“www.nyist.edu.cn” { write_com(0xb8 + x); write_com(0x48); } else if(y == 2) // 第三行居中显示“电子信息工程专业” { write_com(0xb8 + x); write_com(0x50); } else if(y == 3) // 第四行居中显示尚春芳 { write_com(0xb8 + x); write_com(0x58); } for(i = 0; s[i] != '\0'; i++) { write_data(s[i]); } } void main() { init_lcd(); // 初始化LCD display_string(18, 0, "南阳理工学院"); // 显示第一行 display_string(16, 1, "www.nyist.edu.cn"); // 显示第二行 display_string(16, 2, "电子信息工程专业"); // 显示第三行 display_string(22, 3, "尚春芳"); // 显示第四行 while(1); } ``` 以上代码实现了你提出的要求,第一行居中显示“南阳理工学院”,第二行居中显示“www.nyist.edu.cn”,第三行居中显示“电子信息工程专业”,第四行居中显示尚春芳。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值