数据结构实验之二叉树八:(中序后序)求二叉树的深度
来源:互联网 发布:淘宝宝贝首图制作教程 编辑:程序博客网 时间:2024/06/10 13:17
数据结构实验之二叉树八:(中序后序)求二叉树的深度
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
已知一颗二叉树的中序遍历序列和后序遍历序列,求二叉树的深度。
Input
输入数据有多组,输入T,代表有T组数据。每组数据包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历,第二个表示二叉树的后序遍历。
Output
输出二叉树的深度。
Example Input
2dbgeafcdgebfcalnixulinux
Example Output
43
Hint
Author
#include <iostream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
char a[55],b[55];
int i,count1;
struct mode
{
struct mode *l,*r;
char data;
};
struct mode *creat(char a[],char b[],int n)
{
if(n==0)
return NULL;
struct mode *root;
root=(struct mode *)malloc(sizeof(struct mode));
root->data=b[n-1];
int i;
for(i=0;i<n;i++)
{
if(a[i]==root->data)
break;
}
root->l=creat(a,b,i);
root->r=creat(a+1+i,b+i,n-i-1);
return root;
};
void leaf(struct mode *root,int &count1)
{
if(root)
{
if(root->l==NULL&&root->r==NULL)
{
count1++;
}
leaf(root->l,count1);
leaf(root->r,count1);
}
}
int deep(struct mode *root)
{
int d=0;
if(root)
{
int l1=deep(root->l);
int l2=deep(root->r);
if(l1>l2)
d=l1+1;
else
d=l2+1;
}
return d;
}
int main()
{
int n;
int t;
struct mode *root;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%s",a);
scanf("%s",b);
n=strlen(a);
root=creat(a,b,n);
printf("%d\n",deep(root));
}
}
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
using namespace std;
char a[55],b[55];
int i,count1;
struct mode
{
struct mode *l,*r;
char data;
};
struct mode *creat(char a[],char b[],int n)
{
if(n==0)
return NULL;
struct mode *root;
root=(struct mode *)malloc(sizeof(struct mode));
root->data=b[n-1];
int i;
for(i=0;i<n;i++)
{
if(a[i]==root->data)
break;
}
root->l=creat(a,b,i);
root->r=creat(a+1+i,b+i,n-i-1);
return root;
};
void leaf(struct mode *root,int &count1)
{
if(root)
{
if(root->l==NULL&&root->r==NULL)
{
count1++;
}
leaf(root->l,count1);
leaf(root->r,count1);
}
}
int deep(struct mode *root)
{
int d=0;
if(root)
{
int l1=deep(root->l);
int l2=deep(root->r);
if(l1>l2)
d=l1+1;
else
d=l2+1;
}
return d;
}
int main()
{
int n;
int t;
struct mode *root;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%s",a);
scanf("%s",b);
n=strlen(a);
root=creat(a,b,n);
printf("%d\n",deep(root));
}
}
return 0;
}
阅读全文
0 0
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- SDUT-2804 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 2804 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 2804 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- [转载]Nginx中文文档
- 数据结构读书笔记(7)----排序总结
- c指针(二)
- 2017116
- 顺序表的基本操作
- 数据结构实验之二叉树八:(中序后序)求二叉树的深度
- 狼抓兔子 BZOJ1001 平面图最小割转对偶图最短路
- Java题目1
- 1183 编辑距离
- Nessus 升级 2017/11/7日更新
- faster rcnn学习之rpn、fast rcnn数据准备说明
- (笔记)数据结构2.2-堆栈
- poj 3256 Wormholes 最短路Floyd
- 特征选择