[PAT] A1020 Tree Traversals
【题目】
distinct 不同的
postorder 后序的
inorder 中序的
sequence 顺序;次序;系列
traversal 遍历
题目大意:给出二叉树的后序遍历和中序遍历,求层次遍历。
【思路】
参见《算法笔记》
【AC代码】
#include<iostream>
#include<queue>
using namespace std;
#define N 32
struct node {
int data;
node *lchild, *rchild;
};
int in[N], post[N];
int n;
node* tree(int postleft, int postright, int inleft, int inright)
{
if (postleft > postright || inleft > inright)
{
return NULL;
}
node* root = new node;
root->data = post[postright];
int i, k;
for (i = inleft; i <= inright; i++)
if (in[i] == post[postright])
break;
k = i - inleft;
root->lchild = tree(postleft, postleft + k - , inleft, i - );
root->rchild = tree(postleft + k, postright - , i + , inright);
return root;
}
void level(node* root)
{
int output = ;//记录输出了多少个数,因为最后一个数输出之后没有空格。
queue<node*>q;
q.push(root);
while (!q.empty())
{
node* tnode = q.front();
q.pop();
cout << tnode->data;
output++;
if (output < n)
cout << " ";
if (tnode->lchild != NULL)q.push(tnode->lchild);
if (tnode->rchild != NULL)q.push(tnode->rchild);
}
}
int main()
{
cin >> n;
int i;
for (i = ; i < n; i++)
cin >> post[i];
for (i = ; i < n; i++)
cin >> in[i];
node* root = tree(, n - , , n - );
level(root);
return ;
}
[PAT] A1020 Tree Traversals的更多相关文章
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT A1020 Tree Traversals(25)
题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
- PAT 1086 Tree Traversals Again
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT甲级——A1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- A1020. Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
随机推荐
- Spring Cloud(七):服务网关zuul过滤器
上文介绍了Zuul的基本使用与路由功能,本文接着介绍Zuul的核心概念 -- Zuul过滤器(filter). Zuul的功能基本通过Zuul过滤器来实现(类比于Struts的拦截器,只是Struts ...
- 13-Servlet&Request
今日内容: 1. Servlet 2. Request Servlet 1. 概念 2. 步骤 3. 执行原理 4. 生命周期 5. Servlet3.0注解配置 6. Servlet的体系结构 se ...
- golang 运算符
/* 算术运算符 : + - * / % ++ -- 关系运算符 : == != > < >= <= 逻辑运算符 : && || ! 赋值运算符 : = += ...
- 吐槽一下python
关于python,优点有很多.例如,编码灵活,书写随意. 印象最深的就是,Duck Type.也就说,如果使用会走路和会飞来衡量鸭子, 那么如果一个物体,走路像鸭子,飞起来像鸭子,那么它就是鸭子. d ...
- get、post请求参数乱码解决方法(qq:1324981084)
java高级架构师全套vip教学视频,需要的加我qq1324981084 在实际的项目中我们会遇见中文乱码的问题,这个问题是很恶心的事,所以我在这里提供了一些解决中文乱码的方法,希望能给大家一些帮助. ...
- Linux学习Day4:管道符、重定向与环境变量
仅仅是学习Linux系统的命令还不够,只有把多个命令按照自己想要的方式进行组合使用,才能提高工作效率.今天的内容主要是关于如何把命令组合在一起使用,使得输入的命令更准确.更高效,也为接下来的Shell ...
- sed命令简介
sed处理时,有2个缓冲区:[pattern space]和[hold space] sed执行过程: 先读入一行,去掉尾部换行符,存入[pattern space],执行编辑命令. 处理完毕,除非加 ...
- 二、GLTF模型支持
1.安装ColladaToGltf.exe 2. @echo off cd C:\Users\wangc04\Desktop\daeconverting\ColladaToGltfcollada2gl ...
- C#_Excel数据读取与写入_自定义解析封装类_支持设置标题行位置&使用excel表达式收集数据&单元格映射&标题映射&模板文件的参数数据替换
本篇博客园是被任务所逼,而已有的使用nopi技术的文档技术经验又不支持我需要的应对各种复杂需求的苛刻要求,只能自己造轮子封装了,由于需要应对很多总类型的数据采集需求,因此有了本篇博客的代码封装,下面一 ...
- C#设计模式学习笔记:(5)原型模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7640873.html,记录一下学习过程以备后续查用. 一.引言 很多人说原型设计模式会节省机器内存,他们说 ...