PAT 甲级 1020 Tree Traversals (二叉树遍历)

时间:2022-08-26 22:18:36

1020. Tree Traversals (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue> using namespace std;
typedef struct Tree
{
int data;
Tree *lchild;
Tree *rchild;
}a[40];
int post[40];
int in[40];
int n;
int ans[40];
void dfs(int l1,int r1,int l2,int r2,Tree* &root)
{
root=new Tree();
int i;
for( i=l1;i<=r1;i++)
if(in[i]==post[r2])
break;
root->data=post[r2];
if(i==l1)
root->lchild=NULL;
else
dfs(l1,i-1,l2,l2+i-l1-1,root->lchild);
if(i==r1)
root->rchild=NULL;
else
dfs(i+1,r1,r2-(r1-i),r2-1,root->rchild);
}
int cnt;
void bfs(Tree *tree)
{
queue<Tree*> q;
q.push(tree);
while(!q.empty())
{
Tree *root=q.front();
q.pop();
ans[cnt++]=root->data;
if(root->lchild!=NULL)
q.push(root->lchild);
if(root->rchild!=NULL)
q.push(root->rchild);
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%d",&post[i]);
for(int i=1;i<=n;i++)
scanf("%d",&in[i]);
Tree *tree;
dfs(1,n,1,n,tree);
cnt=0;
bfs(tree);
for(int i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",ans[i]);
else
printf("%d ",ans[i]);
}
}
return 0;
}

PAT 甲级 1020 Tree Traversals (二叉树遍历)的更多相关文章

  1. PAT 1020 Tree Traversals&lbrack;二叉树遍历&rsqb;

    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;(二叉树已知后序和中序建树求层序)

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

  3. 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 ...

  4. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  5. 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 ...

  6. 【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 ...

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

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  8. hdu1710&lpar;Binary Tree Traversals&rpar;&lpar;二叉树遍历&rpar;

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  9. 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 ...

随机推荐

  1. NOIP 2013 货车运输 最大生成树加DFS巧妙AC

    #include<set> #include<map> #include<cmath> #include<queue> #include<stac ...

  2. 初识ReactJs(一)

    React的开发背景 ReactJS官网地址:http://facebook.github.io/react/ Github地址:https://github.com/facebook/react J ...

  3. 丙申年把真假美猴王*在容器中跑 ASP&period;NET Core 1&period;0

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. 和我一起学python,基本概念 (life is short &comma;we need python)

    作者:tobecrazy  出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 基本概念 : 常量: 常量名全部大写,如PI 变量: ...

  5. 一起写一个Android图片加载框架

    本文会从内部原理到具体实现来详细介绍如何开发一个简洁而实用的Android图片加载缓存框架,并在内存占用与加载图片所需时间这两个方面与主流图片加载框架之一Universal Image Loader做 ...

  6. 【HDOJ】【3709】Balanced Bumber

    数位DP 题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html dfs的地方没太看懂……(也就那里是重点吧喂!)挖个 ...

  7. 十一、mysql输入安全

    .尽量使用“绑定参数”功能,php中可用pdo进行一系列操作 .php可使用mysql_real_escape_string()函数进行输入过滤:

  8. 九度OJ 1387 斐波那契数列

    题目地址:http://ac.jobdu.com/problem.php?pid=1387 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义 ...

  9. TextView drawablePadding没有效果

    1.当TextView 设置宽度设置为match_parent的时候 TextView drawablePadding没有效果 ,字设置了center位置,但是和左边的图片离开很远 2.当TextVi ...

  10. MySQL 文章目录

    MySQL系列: MySQL CREATE TABLE语法 MySQL 复制表结构 MySQL 对比数据库表结构 MySQL 处理插入过程中的主键唯一键重复值办法 MySQL 启动原理剖析 MySQL ...