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

输入格式:

输入第一行给出一个正整数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. iOS 图片循环滚动(切片效果)

                             #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIAp ...

  2. my.cnf详解

    [client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir ...

  3. [翻译] java NIO 教程---介绍

    原文地址:http://tutorials.jenkov.com/java-nio/index.html Java NIO(new IO)是从java1.4之后的对IO API的另一种选择,即对标准j ...

  4. IDEA 常见文件类型的图标介绍

    官网地址:http://www.jetbrains.com/idea/webhelp/symbols.html        对于各个图标,上图的 Description 写得非常详细,但是有几个还是 ...

  5. J2EE MyBatis使用

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  6. Swift游戏实战-跑酷熊猫 10 视差滚动背景

    原理 实现 勘误 “实现”的视频中有个错误,如下 背景移动时有个错误,看红色部分,近景归位时,第二张图片的下标是1 if arrBG[0].position.x + arrBG[0].frame.wi ...

  7. poj 题目分类(2)

    初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. (5)构造法.(poj329 ...

  8. [转] java书籍(给Java程序猿们推荐一些值得一看的好书 + 7本免费的Java电子书和教程 )

    7本免费的Java电子书和教程 1. Thinking in Java (Third Edition) 本书的作者是Bruce Eckel,它一直都是Java最畅销的免费电子书.这本书可以帮助你系统的 ...

  9. [转]JVM内幕:Java虚拟机详解

    本文由 ImportNew - 挖坑的张师傅 翻译自 jamesdbloom.欢迎加入翻译小组.转载请见文末要求. 这篇文章解释了Java 虚拟机(JVM)的内部架构.下图显示了遵守Java SE 7 ...

  10. Python学习总结6:字符串格式化操作及方法总结

    1. 格式化操作(%) Python中内置有对字符串进行格式化的操作. 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格 ...