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

Time Limit: 1000 ms Memory Limit: 65536 KiB

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 数据结构实验之二叉树八:(中序后序)求二叉树的深度的更多相关文章

  1. SDUT OJ 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  2. SDUT OJ 数据结构实验之排序八:快速排序

    数据结构实验之排序八:快速排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 给定N ...

  3. SDUT OJ 数据结构实验之链表八:Farey序列

    数据结构实验之链表八:Farey序列 Time Limit: 10 ms Memory Limit: 600 KiB Submit Statistic Discuss Problem Descript ...

  4. SDUT 3364 数据结构实验之图论八:欧拉回路

    数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...

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

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  6. SDUT OJ 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  7. SDUT OJ 数据结构实验之二叉树二:遍历二叉树

    数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. SDUT OJ 数据结构实验之二叉树一:树的同构

    数据结构实验之二叉树一:树的同构 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

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

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...

随机推荐

  1. Tornado 高并发源码分析之六---异步编程的几种实现方式

    方式一:通过线程池或者进程池 导入库futures是python3自带的库,如果是python2,需要pip安装future这个库 备注:进程池和线程池写法相同 from concurrent.fut ...

  2. git用法小结(1)--建立远程仓库

    最近一直在学习使用git来管理自己的程序,总是今天东学一点,明天西凑一点,到用的时候,总是有些茫然不知所措. 在博客园里看见一篇老好的文章,教我们做笔记啦,但是做完笔记还是要记得总结哦! 来吧,让我们 ...

  3. Python的Flask框架使用Redis做数据缓存的配置方法

    flask配置redis 首先得下载flask的缓存插件Flask-Cache,使用pip下载. sudo pip install flask_cache 为应用扩展flask_cache   app ...

  4. Unity3D Physics Keynote

    [Unity3D Physics Keynote] 1.在哪设置Layer Collision Matrix? "Edit"->"Project Settings& ...

  5. 02.socket实现远程调用

    不使用webservice使用以前的知识也可以实现远程系统之间的调用.用Socket可以.实现Socket通信. 开设一个端口.ip.

  6. Linux GCC编译警告:Clock skew detected. 错误解决办法

    今天在虚拟机上用GCC编译一个程序的时候,出现了下面的错误: make: warning: Clock skew detected. Your build may be incomplete 试了ma ...

  7. Installing R under Unix-alikes

    Linux上R的安装 可参考https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-Unix_ ...

  8. bootstrap图片切换效果

    <!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8&quo ...

  9. 解析json的方法

    解析json的两种方法:JS中的eval().JSON.parse eval不仅解析内容还会解析其中的方法,JSON.parse解析更安全.JSONLint可校验json的错误.

  10. jQuery中关于toggle的使用

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>t ...