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 ...
随机推荐
- SpringBoot22 Ajax跨域、SpringBoot返回JSONP、CSRF、CORS
1 扫盲知识 1.1 Ajax为什么存在跨域问题 因为浏览器处于安全性的考虑不允许JS执行跨域请求. 1.2 浏览器为什么要限制JS的跨域访问 如果浏览器允许JS的跨域请求就很容易造成 CSRF (C ...
- 682. Baseball Game 棒球游戏 按字母处理
[抄题]: You're now a baseball game point recorder. Given a list of strings, each string can be one of ...
- 下载Redis
1.下载当前Redis 官网:https://redis.io/ 当前稳定版本是4.0.11,如下图,点Download it下面的链接进行下载 2.下载历史版本的Resis 网址: http://d ...
- 浅析junit4及扩展实践
junit框架相关源代码分析,网上已经有很多了,本篇不做过多相关解说,主要还是要自己多读相关源代码.本篇主要对自动化测试过程相关的测试用例,测试数据,测试结果结合junit做相关扩展说明. 如果要解读 ...
- jQuery--左侧菜单收缩隐藏
实现步骤: 步骤一. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...
- CF 961G Partitions
推不动式子 我们考虑每一个$w_i$对答案的贡献,因为题目中定义集合的价值为$W(S) = \left | S \right |\sum_{x \in S}w_x$,这个系数$\left | S \r ...
- sql语句in超过1000时的写法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- exe文件停止运行的情况
1.程序问题. 2.服务器问题. 3.内存占用问题. 一般情况下,关掉程序,重新打开就可以. 上述情况不行,则关掉电脑,重启. 再不行,Ctr + Alt + Del关掉程序的进程. 不行, Win ...
- Tomcat之——内存溢出设置JAVA_OPTS
答案1设置Tomcat启动的初始内存其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4.可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置三.实例,以下 ...
- 阿里云RDS外网无法访问解决办法
为了安全起见,阿里云的RDS数据库设置只能内网连接,那么为了方便查询数据,我每次都去连接内网服务器远程桌面. 这次我因为网络问题,远程桌面非常不稳定,一会掉线一会掉线,只能另想办法. 同事那里有个批处 ...