SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树
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>
struct node
{
int data;
char c;
struct node *lt, *rt;
};
char s1[100],s2[100];
struct node *creat(int n, char s1[], char s2[])
{
int i=0;
struct node *root;
if(n==0) return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->c=s1[0];
for(i=0; i<n; i++){
if(s1[0]==s2[i]){
root->lt=creat(i, s1+1, s2);
root->rt=creat(n-i-1, s1+i+1, s2+i+1);
}
}
return root;
}
/*void fir(struct node *root)
{
if(root){
printf("%c",root->c);
fir(root->lt);
fir(root->rt);
}
}*/
int h(struct node *root)
{
if(root->lt==NULL&&root->rt==NULL){
root->data=1;
}
else if(root->lt==NULL&&root->rt!=NULL){
root->data=h(root->rt)+1;
}
else if(root->lt!=NULL&&root->rt==NULL){
root->data=h(root->lt)+1;
}
else if(root->lt!=NULL&&root->rt!=NULL){
if(h(root->lt)>h(root->rt)){
root->data=h(root->lt)+1;
}
else{
root->data=h(root->rt)+1;
}
}
return root->data;
}
int main()
{
int n;
while(~scanf("%d",&n)){
scanf("%s %s",s1,s2);
struct node *root;
root=creat(n,s1,s2);
//fir(root);
printf("%d\n",h(root));
}
return 0;
}
SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树的更多相关文章
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之排序四:寻找大富翁
数据结构实验之排序四:寻找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Submit Statistic Discuss Problem Descripti ...
- SDUT OJ 数据结构实验之链表四:有序链表的归并
数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT 3401 数据结构实验之排序四:寻找大富翁.!
数据结构实验之排序四:寻找大富翁 Time Limit: 150MS Memory Limit: 512KB Submit Statistic Problem Description 2015胡润全球 ...
- SDUT 3376 数据结构实验之查找四:二分查找
数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...
- SDUT 3361 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有一个地下迷 ...
- SDUT OJ 数据结构实验之二叉树七:叶子问题
数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...
- SDUT OJ 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
随机推荐
- IE回车的一个怪异行为
IE中在input中回车相当于提交form,会从dom中找最近的button标签触发click事件 <!DOCTYPE html> <html> <head> &l ...
- 在cmd中 操作 数据库 MySQL 的一些命令
环境变量配置配置好以后, 打开cmd 连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样) 断开:exit (回车) 创建授权:grant sele ...
- python pipelines 用法
http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html http://blog.csdn.net/m ...
- NOIP2018 解题笔记
D1T1 铺设道路 在场上并没有想到积木大赛这道原题. 差分之后可以把在$[l, r]$这段区间$ - 1$变成在$l$处$ - 1$,在$r + 1$处$ + 1$,然后最终目标是使$\forall ...
- Django框架 之 Cookie、Session整理补充
Django框架 之 Cookie.Session整理补充 浏览目录 Django实现的Cookie Django实现的Session 一.Django实现的Cookie 1.获取Cookie 1 2 ...
- LightOJ 1079 Just another Robbery (01背包)
题意:给定一个人抢劫每个银行的被抓的概率和该银行的钱数,问你在他在不被抓的情况下,能抢劫的最多数量. 析:01背包,用钱数作背包容量,dp[j] = max(dp[j], dp[j-a[i] * (1 ...
- python核心编程第3章课后题答案(第二版55页)
3-4Statements Ues ; 3-5Statements Use\(unless part of a comma-separated sequence in which case \ is ...
- Densely Connected Convolutional Networks(緊密相連卷積網絡)
- Dense blocks where each layer is connected to every other layer in feedforward fashion(緊密塊是指每一個層與每 ...
- 解决iReport打不开的一种方法
解决iReport打不开的一种方法 iReport版本:iReport-5.6.0-windows-installer.exe 系统:Win7 64位 JDK:1.7 在公司电脑安装没问题,能打开,但 ...
- Android使用Canvas画图
1.参考:http://blog.csdn.net/rhljiayou/article/details/7212620/ 2.常用方法: 1.Canvas类 drawArc 绘制弧 drawBitma ...