SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度
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>
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=s2[n-1];
for(i=0; i<n; i++){
if(s1[i]==s2[n-1]){
root->lt=creat(i, s1, s2);
root->rt=creat(n-1-i, s1+i+1, s2+i);
}
}
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;
scanf("%d",&n);
while(n--){
scanf("%s %s",s1,s2);
int len = strlen(s1);
struct node *root;
root=creat(len,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: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...
- SDUT OJ 数据结构实验之链表八:Farey序列
数据结构实验之链表八:Farey序列 Time Limit: 10 ms Memory Limit: 600 KiB Submit Statistic Discuss Problem Descript ...
- SDUT 3364 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...
- SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树
数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT OJ 数据结构实验之二叉树六:哈夫曼编码
数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- 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 Descri ...
随机推荐
- fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory
VS2010编译 DirectShow一些项目时遇到 错误:fatal error C1083: 无法打开包括文件:“qedit.h”: No such file or directory 解决方法: ...
- python:if 语句的使用方法
if-else类型: #if-else num = int(input("输入成绩!")) if num > 60: print ("及格") else: ...
- bash shell笔记7 创建函数
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/525126 知 ...
- bash shell笔记4 处理用…
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/521448 知 ...
- Objective-C 基础语法log打印那些事儿(一)
Objective-C 基础语法详解 雨松MOMO原创文章如转载,请注明:转载至我的独立域名博客雨松MOMO程序研究院,原文地址:http://www.xuanyusong.com/archives/ ...
- java中FILE类常用API介绍
- Docker镜像加速 | Docker 中国源 | 仓库
镜像加速 | Docker 中国https://www.docker-cn.com/registry-mirror
- 安装oracle xe一些注意点
主要是web管理数据的的端口8080端口的问题, 会和tomcat冲突 安装时把开启8080端口的tomcat启动了 占用8080端口就行了, 然后安装oracle xe就会让你输入 web管理数据的 ...
- Oracle与SQL Server实现表数据同步
将SQLServer2008中的某些表同步到Oracle数据库中,不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. 实例1:SQLServer2008有一个表employ_ ...
- 安装express.js(NODEJS框架)
express.js是nodejs的一个MVC开发框架,并且支持jade等多种模板.下面简单来说说express的安装和app.js文件的配置,然后在今后的教程中一步一步使用express.js搭建个 ...