统计利用先序遍历创建的二叉树的深度

10000(ms)
10000(kb)
3331 / 8436
利用先序递归遍历算法创建二叉树并计算该二叉树的深度。先序递归遍历建立二叉树的方法为:按照先序递归遍历的思想将对二叉树结点的抽象访问具体化为根据接收的数据决定是否产生该结点从而实现创建该二叉树的二叉链表存储结构。约定二叉树结点数据为单个大写英文字符。当接收的数据是字符"#"时表示该结点不需要创建,否则创建该结点。最后再统计创建完成的二叉树的深度(使用二叉树的后序遍历算法)。需要注意输入数据序列中的"#"字符和非"#"字符的序列及个数关系,这会最终决定创建的二叉树的形态。

输入

输入为先序遍历二叉树结点序列。

输出

对应的二叉树的深度。

样例输入

A##
ABC####
AB##C##
ABCD###E#F##G##
A##B##

样例输出

1
3
2
4
1
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef int Datetype;
using namespace std;
typedef struct link{
Datetype date;
struct link *lchild;
struct link *rchild;
}tree; void creattree(tree *&L)
{
char c;
cin>>c;
if(c=='#')
L=NULL;
else
{
L = (tree *)malloc(sizeof(tree)) ;
L->date=c;
creattree(L->lchild);
creattree(L->rchild);
}
} void destroytree(tree *&L)
{
if(L!=NULL)
{
destroytree(L->lchild);
destroytree(L->rchild);
free(L);
}
} int deep(tree *L)
{
int ldep,rdep,max;
if(L!=NULL)
{
ldep=deep(L->lchild);
rdep=deep(L->rchild);
max=ldep>rdep?ldep+:rdep+;
return max;
}
else
return ;
} int main()
{
tree *L = NULL;
int x;
creattree(L);
x=deep(L);
cout<<x;
destroytree(L);
return ;
}

swust oj 971的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

  9. [Swust OJ 385]--自动写诗

    题目链接:http://acm.swust.edu.cn/problem/0385/ Time limit(ms): 5000 Memory limit(kb): 65535    Descripti ...

随机推荐

  1. A fine property of the convective terms of axisymmetric MHD system, and a regularity criterion in terms of $\om^\tt$

    In [Zhang, Zujin; Yao, Zheng-an. 3D axisymmetric MHD system with regularity in the swirl component o ...

  2. [物理学与PDEs]第1章习题8 磁场分布 $\ra$ 电流分布

    设在真空中有一圆柱形磁场 $$\bex B(P)=\sedd{\ba{ll} \cfrac{2I}{Cr},&r\geq R,\\ \cfrac{2I}{CR^2}r,&r<R, ...

  3. springMVC中 @RequestParam和@RequestBody的区别

    首先,不可以同时传进@RequestParam和@RequestBody,好像可以传进两个@RequestParam 如果不加@requestparam修饰,相当于 加上@requestparam且各 ...

  4. CCPC-Wannafly Winter Camp Day5 (Div2, onsite) Sorting(线段树)

    题目链接 题意 对序列进行三种操作: 1.区间求和. 2.将区间小于等于$x$的数不改变相对顺序的前提下放到$x$左边,用同样规则将比$x$大的放到右边. 3.将区间大于$x$的数不改变相对顺序的前提 ...

  5. Permission denied的解决办法

    在运行TensorFlow Example的mnist_dataset_intro时出现了Permission denied的问题,这一看就是权限问题. 解决的办法: $ sudo chmod -R ...

  6. 用juery的ajax方法调用aspx.cs页面中的webmethod方法

    首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //.... ...

  7. c#管理文件系统

    using System; using System.Collections.Generic; using System.IO; using static System.Console; /*Syst ...

  8. Django之restframework

    启动流程:引入rest_framework APP 在restframework中,GET数据可以通过request.query_params.get(xxx)获取,post数据可以通过request ...

  9. CF1119C Ramesses and Corner Inversion

    题目地址:CF1119C Ramesses and Corner Inversion 将两个矩阵异或起来,为 \(1\) 的位置就是需要修改的位置 注意到每一次操作都会导致两行和两列上有两个数被修改 ...

  10. Lua中__index和__newindex实践

    [具有默认值的table] 我们都知道,table中的任何字段的默认值都是nil,但是通过元表,我们可以很容易的修改这一规定,代码如下: function setDefault(tb, default ...