给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数N(<=30),是二叉树中结点的个数。第二行给出其中序遍历序列。第三行给出其前序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树反转后的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

7
1 2 3 4 5 6 7
4 1 3 2 6 5 7

输出样例:

4 6 1 7 5 3 2

勉强敲出来了,包括已知中序和前序建树求层序遍历树的序列,虽然还有两组数据没有过,但是也够了。

O(∩_∩)O哈哈~

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdlib> using namespace std;
const int maxn = ;
int n, num;
string a, b;
typedef struct node{
int data ;
struct node *lchild, *rchild;
}*BiTree, BiNode; void Creat_Tree(BiTree & T, string a, string b){
if( b.length() == ){
T = NULL ;
return ;
}
char root_Node = b[];
int index = a.find(b[]);
string l_a = a.substr(,index);
string r_a = a.substr(index+);
int l_len = l_a.length();
int r_len = r_a.length();
string l_b = b.substr(,l_len);
string r_b = b.substr(+l_len); T = (BiTree)malloc(sizeof(BiNode));
if( T!=NULL){
T -> data = root_Node - ;
Creat_Tree(T->lchild, l_a, l_b);
Creat_Tree(T->rchild, r_a, r_b);
}
} int main(){
BiTree T;
cin >> n ;
if( n == ) return ;
for(int i=; i<n; i++){
cin >> num ;
a.push_back(num + '');
}
for(int i=; i<n; i++){
cin >> num ;
b.push_back(num+'');
} Creat_Tree(T,a,b);
queue<BiTree> q;
q.push(T);
bool flag = true ;
while( !q.empty() ){
BiTree m = q.front();
if( flag ){
cout << m->data ;
flag = false ;
}
else{
cout << " " << m->data ;
}
if( m->rchild ) q.push(m->rchild);
if( m->lchild ) q.push(m->lchild);
q.pop();
}
cout << endl ; return ;
}

加个正确的解答吧。完美AC的。

来源:http://blog.csdn.net/idealism_xxm/article/details/51584798

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAXN=; int n,cnt,root;
int inod[MAXN],preod[MAXN];
int q[],head,tail; struct Node {
int lson,rson,num;
}tr[MAXN]; int dfs(int pl,int pr,int il,int ir) {
if(pl==pr) {
tr[cnt].lson=tr[cnt].rson=-;
tr[cnt].num=preod[pl];
return cnt++;
}
for(int i=il;i<=ir;++i) {
if(preod[pl]==inod[i]) {
int cur=cnt++;
tr[cur].lson=tr[cur].rson=-;
tr[cur].num=preod[pl];
if(il<i) {
tr[cur].lson=dfs(pl+,pl+i-il,il,i-);
}
if(i<ir) {
tr[cur].rson=dfs(pl+i-il+,pr,i+,ir);
}
return cur;
}
}
return cnt;
} int main() {
while(==scanf("%d",&n)) {
for(int i=;i<n;++i) {
scanf("%d",inod+i);
}
for(int i=;i<n;++i) {
scanf("%d",preod+i);
}
cnt=;
root=dfs(,n-,,n-);
head=tail=;
if(tr[root].rson!=-) {
q[tail++]=tr[root].rson;
}
if(tr[root].lson!=-) {
q[tail++]=tr[root].lson;
}
printf("%d",tr[root].num);
while(head!=tail) {
printf(" %d",tr[q[head]].num);
if(tr[q[head]].rson!=-) {
q[tail++]=tr[q[head]].rson;
}
if(tr[q[head]].lson!=-) {
q[tail++]=tr[q[head]].lson;
}
++head;
}
printf("\n");
}
return ;
}

ACM题目————玩转二叉树的更多相关文章

  1. 团体程序设计天梯赛-练习集L2-011. 玩转二叉树

    L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...

  2. 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树

    L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  3. 九度oj题目1521:二叉树的镜像

    题目1521:二叉树的镜像 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2061 解决:560 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF ...

  4. pat 团体天梯赛 L2-011. 玩转二叉树

    L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...

  5. L2-011. 玩转二叉树(不建树)

    L2-011. 玩转二叉树   给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整 ...

  6. 九度oj 题目1078:二叉树遍历

    题目1078:二叉树遍历 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5326 解决:3174 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历 ...

  7. 【PAT-二叉树】L2-011. 玩转二叉树- 仅仅开100大的数组模拟即可!

    L2-011. 玩转二叉树 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.(我的分析:无非就是说把左子树当成 ...

  8. 【九度OJ】题目1113:二叉树 解题报告

    [九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...

  9. 【九度OJ】题目1078:二叉树遍历 解题报告

    [九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...

随机推荐

  1. Java基础之一组有用的类——Observable和Observer对象(Horrific)

    控制台程序. Obserable类提供了一个有趣的机制,可以把类对象中发生的改变通知给许多其他类对象. 对于可以观察的对象来说,类定义中需要使用java.util.Observable类.只需要简单地 ...

  2. C++Primer 第十四章

    //1.当运算符作用于类类型运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令程序更加易于编写和阅读. //2.重载的运算符是具有特殊名字的函数,它们由关键字operato ...

  3. For嵌套输出图形

    /*输出此图形    *   * *  * * * * * * ** * * * *  * * * *   * * *   * *     *解析:可以把此图形看成两部分----*---* *--* ...

  4. SQL between查询 范围查询

    --sal为员工工资 select * from emp;

  5. How to export a template in Visual Studio?

    Create a customize template file: 1.template arguments introduction like: 上图只是其中一部分,更多请查看文后的参考资源 tem ...

  6. 每天一个java基础知识--static

    内存总体一共分为了 4个部分(stack segment.heap segment.code segment.data segment) 当我们在程序中,申明一个局部变量的时候,此变量就存放在了 st ...

  7. 数组有没有length()这个方法? String有没有length()这个方法?

    答:数组和string都没有Length()方法,只有Length属性.

  8. 结构体. ->操作符的内涵

    实质上就是结构体成员相对于结构体大变量的偏移地址, 操作符所干的事情就是寻址.是偏移,是偏移,是偏移.偏移后的地址

  9. HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)

    Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...

  10. 常见http状态

    200(成功):服务器已成功处理了请求.通常,这表示服务器提供了请求的网页.如果是对您的 robots.txt 文件显示此状态码,则表示 Googlebot 已成功检索到该文件. 304(未修改):自 ...