【PAT】1020. Tree Traversals (25)

时间:2022-08-26 22:27:00

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

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

Sample Output:

4 1 6 3 5 7 2

分析:考察树的建立和遍历。

课参考《编程之美》3.9

#include<iostream>
#include<map>
#include<vector>
#include<queue>
using namespace std; struct Node{
Node *left;
Node *right;
int value;
Node():left(NULL),right(NULL){}
}; void Rebuild(int * PostOrder, int * InOrder, int len, Node* &root){
//判断何时结束递归
if(PostOrder == NULL || InOrder == NULL)
{
root = NULL;
return ;
}
if(root == NULL) root = new Node;
root->value = *(PostOrder + len - 1);
root->left = NULL;
root->right = NULL;
if(len == 1)
return; int count = 0;
int *temp = InOrder;
while(*temp != *(PostOrder + len -1))
{
count ++;
temp++;
if(count > len) break;
}
int left = temp - InOrder ;
int right = len - left - 1;
if(left > 0)
Rebuild(PostOrder, InOrder, left, root->left);
if(right > 0)
Rebuild(PostOrder + left, InOrder+left+1, right, root->right);
} int main()
{
int n,i,t;
while(cin>>n)
{
int *PostOrder = new int[n];
int *InOrder = new int[n];
for(i=0; i<n; i++)
cin>>PostOrder[i];
for(i=0; i<n; i++)
cin>>InOrder[i]; Node *root = new Node;
int post_start,in_start;
post_start = 0;
in_start = 0;
Rebuild(PostOrder, InOrder, n, root);
queue<Node *> q;
q.push(root);
int flag = 1; while(!q.empty()){
if(q.front()->left != NULL)
q.push(q.front()->left);
if(q.front()->right != NULL)
q.push(q.front()->right);
if(flag != n)
cout<<q.front()->value<<" ";
else
cout<<q.front()->value;
flag ++;
q.pop();
} cout<<endl;
}
return 0;
}

【PAT】1020. Tree Traversals (25)的更多相关文章

  1. 【PAT】1020 Tree Traversals &lpar;25&rpar;(25 分)

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  2. PAT 甲级 1020 Tree Traversals &lpar;25分&rpar;(后序中序链表建树,求层序)&ast;&ast;&ast;重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  3. PAT 甲级 1020 Tree Traversals &lpar;25 分&rpar;(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  4. PAT Advanced 1020 Tree Traversals &lpar;25 分&rpar;

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. 【PAT甲级】1020 Tree Traversals &lpar;25 分&rpar;(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  6. PAT Advanced 1020 Tree Traversals &lpar;25&rpar; &lbrack;⼆叉树的遍历,后序中序转层序&rsqb;

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  7. PAT 甲级 1020 Tree Traversals &lpar;二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  8. PAT &lpar;Advanced Level&rpar; 1020&period; Tree Traversals &lpar;25&rpar;

    递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...

  9. 1020&period; Tree Traversals &lpar;25&rpar;

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1020 and the ...

随机推荐

  1. 如何解决grails2&period;3&period;2中不能运行fork模式

    升级到grails 2.3.2之后,运行时报如下的异常: Exception in thread "main" Error | Forked Grails VM exited wi ...

  2. HBase MultiVersionConsistencyControl

    注明:本文部分文字和图片翻译或引用自http://blogs.apache.org/hbase/entry/apache_hbase_internals_locking_and. HBase在保证高性 ...

  3. 引用WCF地址报下载&OpenCurlyDoubleQuote;https&colon;&sol;&sol;xxx&colon;8004&sol;TerminalHandler&period;svc&quest;disco”时出错问题

    服务发布了wcf服务后,在客户端引用发现出现以下错误 - 来自“DISCO 文档”的报告是“下载“https://servername:8004/TerminalHandler.svc?disco”时 ...

  4. Java知IO

    ---恢复内容开始--- Java将IO(文件.网络.终端)封装成非常多的类,看似繁杂,其实每个类的具有独特的功能. 按照存取的对象是二进制还是文本,java使用字节流和字符流实现IO. 流是java ...

  5. DrawerLayout案例

    布局文件: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget ...

  6. java网络编程小白教程

    1 网络编程 1.1 网络 把多台终端(计算机)通过物理线路连接起来,形成网络.便于交换数据.共享信息.组成更强大的逻辑体. 1.1.1 网络通信三要素 [1]IP地址:唯一标识网络上的每一台计算机 ...

  7. ASP&period;NET如何下载大文件

    关于此代码的几点说明: 1. 将数据分成较小的部分,然后将其移动到输出流以供下载,从而获取这些数据. 2. 根据下载的文件类型来指定 Response.ContentType .(参考OSChina的 ...

  8. mybatis之接口方法多参数的三种实现方式

    关键代码举例: DaoMapper.xml <!-- 传入多个参数时,自动转换为map形式 --> <insert id="insertByColumns" us ...

  9. std&colon;&colon;string&colon;&colon;find&lpar;&rpar; 和 std&colon;&colon;string&colon;&colon;npos

    npos是一个常数,用来表示不存在的位置,string::npos代表字符串到头了结束了.   int idx = str.find("abc");if (idx == strin ...

  10. SqlServer批量插入(SqlBulkCopy、表值参数)

    之前做项目需要用到数据库的批量插入,于是就研究了一下,现在做个总结. 创建了一个用来测试的Student表: CREATE TABLE [dbo].[Student]( [ID] [int] PRIM ...