求二叉树的深度
来源:互联网 发布:云计算技术原理 编辑:程序博客网 时间:2024/05/14 20:41
求二叉树的深度
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
已知一颗二叉树的中序遍历序列和后序遍历序列,求二叉树的深度。
输入
输入数据有多组,输入T,代表有T组数据。每组数据包括两个长度小于50的字符串,第一个字符串表示二叉树的中序遍历,第二个表示二叉树的后序遍历。
输出
输出二叉树的深度。
示例输入
2dbgeafcdgebfcalnixulinux
示例输出
43
#include <stdio.h>#include <stdlib.h>#include <string.h>struct node{ char a; struct node *l,*r;}*tree;char s[10001],s1[10001];struct node *creat(struct node *root,char *s,char *s1,int n){ if(n<=0) return NULL; root=(struct node*)malloc(sizeof(struct node)); root->a=s[n-1]; int i; for(i=0;i<n;i++) if(s1[i]==s[n-1]) break; root->l=creat(root->l,s,s1,i); root->r=creat(root->r,s+i,s1+i+1,n-i-1); return root;}int treedeep(struct node *root){ int aa,ll,rr; aa=ll=rr=0; if(root->l==NULL&&root->r==NULL) return 1; if(root->l!=NULL) ll=treedeep(root->l); if(root->r!=NULL) rr=treedeep(root->r); aa=ll>rr?ll:rr; return aa+1;}int main(){ int T; scanf("%d",&T); while(T--) { struct node *head; scanf("%s %s",s1,s); int l=strlen(s); head=creat(head,s,s1,l); int k=treedeep(head); printf("%d\n",k); } return 0;}
0 0
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 求二叉树的深度
- 围观围观
- 使用VC库函数中的快速排序函数
- go 语言基础
- 多线程笔试面试题汇总
- Android 自定义组件<总结篇>
- 求二叉树的深度
- 《设计模式之禅》——观察者模式
- 2015-1-26工作日志-王亮晶
- 批量下载宝宝吧故事集工具
- Core Java笔记—第4章 对象与类
- 【linux学习日记】Windows_grub引导的修复
- Kafka的Producer和Consumer源码学习
- LeetCode - Palindrome Partitioning II 题解
- patch文件的格式(ns2的)