输出利用先序遍历创建的二叉树中的指定结点的双亲结点

1000(ms)
10000(kb)
2415 / 5575
利用先序递归遍历算法创建二叉树并输出该二叉树中指定结点的双亲结点。约定二叉树结点数据为单个大写英文字符。当接收的数据是字符“#”时表示该结点不需要创建,否则创建该结点。最后再输出创建完成的二叉树中的指定结点的双亲结点。注意输入数据序列中的“#”字符和非“#”字符的序列及个数关系,这会最终决定创建的二叉树的形态。

输入

输入用例分2行输入,第一行接受键盘输入的由大写英文字符和“#”字符构成的一个字符串(用于创建对应的二叉树),第二行为指定的结点数据。

输出

用一行输出该用例对应的二叉树中指定结点的双亲结点。若相应双亲结点不存在则以“#”代替。

样例输入

A##
A
ABC####
B

样例输出

#
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cstdio>
typedef char Datetype;
using namespace std;
char x; typedef struct link{
Datetype date;
struct link *lchild;
struct link *rchild;
}tree; typedef struct queue{
tree *data;
struct queue *next;
}que; typedef struct {
que *front;
que *rear;
}lin; void Initqueue(lin *&L)
{
L=(lin *)malloc(sizeof(lin));
L->front=L->rear=NULL;
} void destroyed(lin *&L)
{
que *p=NULL,*r=NULL;
p=L->front;
while(p!=NULL)
{
r=p;
p=p->next;
free(r);
}
free(L);
} bool pop(lin *&L, tree *&e)
{
que *p;
if(L->rear==NULL)
return false;
p=L->front;
if(L->rear==L->front)
L->front=L->rear=NULL;
else
L->front=p->next;
e=p->data;
free(p);
return true;
} int empty(lin *&L)
{
return (L->rear==NULL);
} void push(lin *&L,tree *e)
{
que *p;
p = (que *)malloc(sizeof(que));
p->data=e;
p->next=NULL;
if(L->rear==NULL)
{
L->front=p;
L->rear=p;
}
else
{
L->rear->next=p;
L->rear=p;
}
} 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 find(tree *L)
{
if(L!=NULL)
{
x++;
find(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 ;
} void finddegree(tree *&L, int n)
{
if(L!=NULL)
{
if(x<n)
x=n;
finddegree(L->lchild,n);
finddegree(L->rchild,n+);
} } void run(tree *L)
{
tree *p=L;
lin *qu;
Initqueue(qu);
if(L!=NULL)
push(qu,p);
while(!empty(qu))
{
pop(qu,p);
cout<<p->date;
if(p->lchild!=NULL)
push(qu,p->lchild);
if(p->rchild!=NULL)
push(qu,p->rchild);
}
destroyed(qu);
} void displayhou(tree *&L)
{
if(L!=NULL)
{
displayhou(L->lchild);
displayhou(L->rchild);
cout<<L->date;
}
} void displaypre(tree *&L)
{
if(L!=NULL)
{
cout<<L->date;
displaypre(L->lchild);
displaypre(L->rchild);
}
} void creatinpre(tree *&L ,char *in,char *pre,int x)
{
int k=;
char *p;
if(x<=)
{
L=NULL;
return ;
}
L=(tree *)malloc(sizeof(tree));
L->date = *pre;
for(p=in ; p<in+x; p++)
{
if(*p==*pre)
break;
}
k=p-in;
creatinpre(L->lchild,in,pre+,k);
creatinpre(L->rchild,p+,pre+k+,x-k-);
} void creatinhou(tree *&L ,char *in,char *pre,int x)
{
int k=;
char *p;
if(x<=)
{
L=NULL;
return ;
}
L=(tree *)malloc(sizeof(tree));
L->date = *pre;
for(p=in ; p>in-x; p--)
{
if(*p==*pre)
break;
}
k=in-p;
creatinhou(L->rchild,in,pre-,k);
creatinhou(L->lchild,p-,pre-k-,x-k-);
} void findson(tree *&L)
{
if(L!=NULL)
{
if(L->date==x)
{
if(L->lchild==NULL)
cout<<"L:#";
else
cout<<"L:"<<L->lchild->date;
if(L->rchild==NULL)
cout<<",R:#";
else
cout<<",R:"<<L->rchild->date;
return ;
}
findson(L->lchild);
findson(L->rchild);
}
} void finddad(tree *&L)
{
if(L!=NULL)
{
if(L->lchild!=NULL&&L->lchild->date==x||L->rchild!=NULL&&L->rchild->date==x)
{
cout<<L->date;
return ;
} finddad(L->lchild);
finddad(L->rchild);
}
} int main()
{
tree *L = NULL;
creattree(L);
cin>>x;
if(x==L->date)
cout<<"#";
else
finddad(L);
destroytree(L);
return ;
}

swust oj 1052的更多相关文章

  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. Blocking Cross Origin API request for /api/contents Creating Notebook Failed An error occurred while creating a new notebook.

    anacoda安装的jupyter,使用nginx进行了转发,远程访问可以进去,但是创建文件和创建目录都会报错 浏览器页面报错: 第一次使用jupyter创建python时错误:Creating No ...

  2. Uni-app事件处理

    事件表: Web事件 uni-app事件 click tap touchstart touchstart touchmove touchmove touchcancel touchcancel tou ...

  3. CISPA Scyther tools

    1.Scyther软件作者网站的整理 Scyther工具的网站主页:https://people.cispa.io/cas.cremers/index.html 首先 对Scyther软件的资料进行整 ...

  4. md5两次加密

    private static final String salt="hzjfstkfdff"; public static String MD5(String src) { ret ...

  5. 公设基础equals

    1#  覆盖equals方法的通用约定 1.自反性(reflexive) 自己跟自己的比较必须返回true 2.对称性(symmetric) x=y那么y=z 3.传递性(transitive) x= ...

  6. UMeditor控制多张图片上传顺序

    多张图片上传的顺序,受到用户使用习惯.插件上传和插件插入页面顺序的影响. 估计是考虑到上传性能,官方没有提供UMeditor控制展示顺序的配置.在上传过程中,用户点击拖动的第一张图片,将作为文件数组中 ...

  7. Linq to Object原理

    using System; using System.Collections.Generic; using System.Linq; using System.Threading; namespace ...

  8. python常见排序算法解析

    python——常见排序算法解析   算法是程序员的灵魂. 下面的博文是我整理的感觉还不错的算法实现 原理的理解是最重要的,我会常回来看看,并坚持每天刷leetcode 本篇主要实现九(八)大排序算法 ...

  9. kafka常规及几个重要的操作命令

    1. 查看所有topic kafka-topics.sh --zookeeper hadoop3 --list 2. 创建tooic及topic的partitioner ./kafka-topics. ...

  10. SLAM学习--开源测试数据集合

    Tum RGB-D SLAM Dataset and Benchmark https://vision.in.tum.de/data/datasets/rgbd-dataset Kitti http: ...