[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 ...
随机推荐
- CentOS使用Postfix发送邮件
1)配置hosts映射 [root@mail ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mt ...
- Chrome恢复显示网址 https:// 和 www
文章来自我的博客: https://blog.ljyngup.com/archives/686.html/ Chrome这个新规弄得我也很蛋疼,每次一点输入框就突然突出来一下.后来在Hostloc论坛 ...
- Django (二) 常用字段及 ORM
MVC介绍 Django生命周期 many-to-many One-to-many Django常用字段 CharFiled 需要有max_length unique=True(代表不能重名) Ema ...
- FastDFS 配置文件 client.conf storage_ids.conf
client.conf : # connect timeout in seconds # default value is 30s connect_timeout=30 连接 ...
- light oj 1214 - Large Division 大数除法
1214 - Large Division Given two integers, a and b, you should check whether a is divisible by b or n ...
- python2 + Django 中文传到模板页面变Unicode乱码问题
1.确保views页面首行设置了默认编码 # -*-coding:utf-8 -*- 2.确保html页面的编码为 utf-8 3.确保项目setting文件设置了 LANGUAGE_CODE = ...
- 实践:使用了CompletableFuture之后,程序性能提升了三倍
CompletableFuture 相比于jdk5所提出的future概念,future在执行的时候支持异步处理,但是在回调的过程中依旧是难免会遇到需要等待的情况. 在jdk8里面,出现了Comple ...
- Spark SQL 之自定义删除外部表
前言 Spark SQL 在删除外部表时,本不能删除外部表的数据的.本篇文章主要介绍如何修改Spark SQL 源码实现在删除外部表的时候,可以带额外选项来删除外部表的数据. 本文的环境是我一直使用的 ...
- jQuery on 绑定的事件 执行两次
$(".class1").on("click",".class2",function(){ alert('提示'); }); 上面代码,怎么 ...
- Android项目实战(五十九):调试方法神器Hugo
先上GitHub地址:Hugo 一句话描述该框架,以Log的形式告诉我们某个方法传入的每一个参数以及返回值,以及调用的activity 时间 等其他信息 非常适用方便代码调试 引入框架: 根目录下的b ...