PAT A1020 Tree Traversals (25 分)——建树,层序遍历
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
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <algorithm>
- #include <iostream>
- #include <string.h>
- #include <queue>
- #include <string>
- #include <set>
- #include <map>
- using namespace std;
- const int maxn = ;
- int n;
- int post[maxn], in[maxn];
- struct node {
- int data;
- node* left;
- node* right;
- };
- void layerorder(node* root) {
- queue<node*> q;
- q.push(root);
- int count = ;
- while (!q.empty()) {
- node* now = q.front();
- q.pop();
- printf("%d", now->data);
- count++;
- if (now->left != NULL) q.push(now->left);
- if (now->right != NULL) q.push(now->right);
- if (count != n) printf(" ");
- }
- }
- node* create(int postl, int postr, int inl, int inr) {
- if (postl > postr) {
- return NULL;
- }
- node* root = new node;
- root->data = post[postr];
- int k;
- for (k = inl; k <= inr; k++) {
- if (in[k] == post[postr]) {
- break;
- }
- }
- int leftnum = k - inl;
- root->left = create(postl, postl + leftnum - , inl, k-);
- root->right = create(postl + leftnum, postr - , k + , inr);
- return root;
- }
- int main() {
- cin >> n;
- for (int i = ; i < n; i++) {
- cin>>post[i];
- }
- for (int i = ; i < n; i++) {
- cin >> in[i];
- }
- node* root = create(, n - , , n - );
- layerorder(root);
- system("pause");
- }
注意点:考察基本的二叉树遍历,难点在递归上,想清楚了递归边界和递归式就简单了。二叉树的遍历及建树还需要巩固。
PAT A1020 Tree Traversals (25 分)——建树,层序遍历的更多相关文章
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- A1020. Tree Traversals(25)
这是一题二叉树遍历的典型题,告诉我们中序遍历和另外一种遍历序列,然后求任何一种遍历序列. 这题的核心: 建树 BFS #include<bits/stdc++.h> using names ...
- [PAT] A1020 Tree Traversals
[题目] distinct 不同的 postorder 后序的 inorder 中序的 sequence 顺序:次序:系列 traversal 遍历 题目大意:给出二叉树的后序遍历和中序遍历,求层次遍 ...
随机推荐
- 【Quartz】1、Quartz使用说明
简介 Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.Quartz 允许开发人员根据时间间隔(或天)来调度作业.它实现了作业和触发器的多对多关系,还 ...
- csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate
Domain: FluentNhibernateLocalSessionFactoryObject.cs using System; using System.Collections.Generic; ...
- 【读书笔记】iOS-单元测试工具
一,单元测试工具. 1,OCUnit 苹果自带的. 2,GHUnit GHUnit是Objective-C语言里的另外一种单元测试工具. GHUnit- https://github.com/gh-u ...
- 使用jquery获取iframe内的元素属性
当需要获取iframe里的内容时需要有几个前提,否则你是获取不到的: 1:当前页面与iframe的src的页面需要在同一个域名下: 2:必须要等iframe里边的页面加载完成才能获取,否则你要获取的标 ...
- Mongodb的入门(8)mongodb事物分析
老生常谈:<在前面博客中也介绍过> mongodb官网:https://docs.mongodb.com/manual/introduction/ mongodb:官网上是这样定义的Mon ...
- 用LinQ扩展方法,泛型扩展方法,实现自定义验证字符是否空、对象是否为null,及泛型约束使用,Action的使用
一.Linq扩展方法 1.扩展方法必须是静态方法.扩展方法所在的类必须是静态类 2.扩展方法里面的参数必须制定this关键字,紧跟需要扩展的类型,如下: 二.泛型约束 1.使用泛型的原因,是在不知道需 ...
- Android 发送自定义广播(标准和本地)
1.首先自定义一个广播接收器:MyBroadcastReceiver package example.com.mybroadcastreceiver; import android.content.B ...
- php二维数组去重
php二维数组去重 前言:php一维数组去重很简单,直接array_unique($arr)即可,但是二维数组去重就得自己去写了 二维数组去重方法: /* * 二维数组去重 * 注意:二维数组中的元素 ...
- Ubuntu关闭(重启)网络服务命令
Ubuntu关闭网络服务命令: service network-manager stop 重启网络服务命令: service network-manager restart
- 彻底卸载删除Win10易升,禁止再生
易升是微软推出的win10升级工具.用户可通过易升一键升级win10. 因为我的电脑已经是win10的系统,所以我也不需要升级.也不想升级,因为我从网上了解到升级后的系统反而没有升级前的好用. 微软的 ...