ACM题目————玩转二叉树
给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列。所谓镜面反转,是指将所有非叶结点的左右孩子对换。这里假设键值都是互不相等的正整数。
输入格式:
输入第一行给出一个正整数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题目————玩转二叉树的更多相关文章
- 团体程序设计天梯赛-练习集L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- 团体程序设计天梯赛 L2-006. 树的遍历 L2-011. 玩转二叉树
L2-006. 树的遍历 #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...
- 九度oj题目1521:二叉树的镜像
题目1521:二叉树的镜像 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2061 解决:560 题目描述: 输入一个二叉树,输出其镜像. 输入: 输入可能包含多个测试样例,输入以EOF ...
- pat 团体天梯赛 L2-011. 玩转二叉树
L2-011. 玩转二叉树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜 ...
- L2-011. 玩转二叉树(不建树)
L2-011. 玩转二叉树 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.这里假设键值都是互不相等的正整 ...
- 九度oj 题目1078:二叉树遍历
题目1078:二叉树遍历 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5326 解决:3174 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历 ...
- 【PAT-二叉树】L2-011. 玩转二叉树- 仅仅开100大的数组模拟即可!
L2-011. 玩转二叉树 给定一棵二叉树的中序遍历和前序遍历,请你先将树做个镜面反转,再输出反转后的层序遍历的序列.所谓镜面反转,是指将所有非叶结点的左右孩子对换.(我的分析:无非就是说把左子树当成 ...
- 【九度OJ】题目1113:二叉树 解题报告
[九度OJ]题目1113:二叉树 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1113 题目描述: 如上所示,由正整数1,2,3-- ...
- 【九度OJ】题目1078:二叉树遍历 解题报告
[九度OJ]题目1078:二叉树遍历 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1078 题目描述: 二叉树的前序.中序.后序遍历 ...
随机推荐
- UILabel属性
1.text:设置标签显示文本. 2.attributedText:设置标签属性文本. Ios代码 NSString *text = @"first"; NSMutableAttr ...
- TNS-01189: The listener could not authenticate the user
查看监听时,发现监听状态异常,报TNS-01189: The listener could not authenticate the user错误 $ lsnrctl stat LSNRCTL - P ...
- Simple GDB case
to be added... gdb a.out [Inferior 1 (process 9718) exited with code 05] (gdb) list Line number ...
- SSM框架搭建
http://blog.csdn.net/gebitan505/article/details/44455235/
- Static Const
Static 内部的 Const 不可变的 一般写法 在.m文件中, static NSString *const ID = @"shop"; static const CGFlo ...
- <s:iterator> 对list操作的一种方法
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp
- table创建固定表头
布局:两个div,上部内容将表头复制,高度固定,下部div内部将table设置为margin:-**px; 隐藏掉表头,下部div设置overflow,即可. 代码:
- extjs中的下载并对文件重命名功能的实现
在小白的学习extjs的过程中,如果需要了解多文件的上传功能,也可以查看小白的上篇随笔,希望给大家帮助.http://www.cnblogs.com/wangqc/p/extjsFileUpload. ...
- ArrayList和LinkList区别
ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除
- struts配置请求后缀,将.action改为.do、.doaction_2015.01.04
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...