数据结构实验之二叉树八:(中序后序)求二叉树的深度(SDUT 2804)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data ;
struct node *l,*r;
};
struct node *creat(char *inorder,char *postorder,int len)
{
struct node *root;
if (len <= 0)
return NULL;
int i = 0;
root = (struct node *)malloc(sizeof (struct node ));
root -> data = *(postorder + len -1);
for (i = 0; i < len; i ++)
{
if (inorder[i] == *(postorder + len - 1))
break;
}
root -> l = creat(inorder, postorder, i);
root -> r = creat(inorder + i + 1,postorder + i,len - i - 1);
return root;
}
int fin(struct node*root)
{
int h=0;
int d1,d2;
if (root)
{
d1 = fin(root -> l);
d2 = fin(root -> r);
h = d1>d2?d1+1:d2 + 1;
}
return h;
}
int main ()
{
int t,len;
char inorder[100],postder[100];
struct node *tree;
while (~scanf ("%d",&t))
{
while (t--)
{
scanf ("%s%s",inorder,postder);
len =strlen (postder);
tree=creat(inorder,postder,len);
int h = fin(tree);
printf ("%d\n",h);
}
}
return 0;
}
数据结构实验之二叉树八:(中序后序)求二叉树的深度(SDUT 2804)的更多相关文章
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- URAL 1136 Parliament 二叉树水题 BST后序遍历建树
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- 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 3364 数据结构实验之图论八:欧拉回路
数据结构实验之图论八:欧拉回路 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 在哥尼斯堡的 ...
随机推荐
- Nuget常用命令(转)
转自:https://www.cnblogs.com/xcsn/p/6259853.html CMD将nuget升级到最新版本:nuget update -self 一.安装 1.安装指定版本类库in ...
- nodejs 对 png 图片的像素级别处理
使用node对图片的像素进行处理 这里使用常见的图片灰度处理为例子: 用到了 pngjs 的 npm 库,这个库可以将 png 的图片文件 Buffer 流,转换为 r g b a 为 255 的像素 ...
- Tokitsukaze and Duel CodeForces - 1191E (博弈论)
大意: 给定01串, 两人轮流操作, Tokitsukaze先手. 每次操作可以选择长为$k$的区间, 全部替换为$0$或$1$, 若替换后同色则赢. 求最后结果. 先判断第一步是否能直接赢, 不能的 ...
- IOS 主队列,全局队列的关系
同步,异步,串行,并发 同步和异步代表会不会开辟新的线程.串行和并发代表任务执行的方式. 同步串行和同步并发,任务执行的方式是一样的.没有区别,因为没有开辟新的线程,所有的任务都是在一条线程里面执行. ...
- 基于【 centos7】二 || 系统时间与网络时间同步
# date // 查看系统时间 #hwclock // 查看硬件时间 # yum -y install ntp ntpdate 安装ntpdate工具 # ntpdate cn.pool.ntp.o ...
- vue html属性绑定
业务场景: element ui里面select的组件,通过勾选是否启动搜索功能 解决: 在select组件上绑定一个 :filterable,值就是是否可搜索的checkbox的值 true和fa ...
- oracle exists和 not exists 的用法
比如 a,b 关联列为 a.id = b.id,现在要取 a 中的数据,其中id在b中也存在: select * from a where exists(select 1 from b where b ...
- xposed获取context 的方法
// 应用被加壳,采用这种方式加载类 try { XposedHelpers.findAndHookMethod(Application.class, "attach", Cont ...
- sql语句查询结果中添加自增列
SELECT Row_Number() over ( order by getdate() ) as init , * FROM 表名
- Linux Ubuntu XShell连接虚拟机问题记录
我们先用ip addr / ifconfig查看虚拟机ip地址,然后到windows下的cmd中ping 一下对应地址 一般是可以ping通的. 然后用Xshell或者其他工具连接虚拟机. 如果连不上 ...