[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 ...
随机推荐
- 《数据结构与算法分析-Java语言描述》 分享下载
书籍信息 书名:<数据结构与算法分析-Java语言描述> 原作名:Data Structures and Algorithm Analysis in Java 作者: 韦斯 (Mark A ...
- Jmeter之将测试结果导出到Excel
一:环境准备 1.下载jxl.jar这个jar包 2.下载好之后,放到Jmeter的安装路径下的lib目录下 3.jxl.jar的作用:完成对Excel的读写以及修改操作 如何利用jmter操作exc ...
- 使用newtonsoft完美序列化WebApi返回的ValueTuple
由于开发功能的需要,又懒得新建太多的class,所以ValueTuple是个比较好的偷懒方法,但是,由于WebApi需要返回序列化后的json,默认的序列化只能将ValueTuple定义的各个属性序列 ...
- Visual C# 2015调用SnmpSharpNet库实现简单的SNMP元素查询
一开始调研发现有几个SNMP的库, 一个是net-SNMP,这个好像是linux用的多 一个是微软自己的WinSNMP,这个没有例子,不太好操作 一个是SnmpSharpNet,这个有些例子比较好, ...
- centos7安装mysql-5.6.43二进制包
卸载老版本的MySQL.查找并删除mysql有关的文件 # find / -name mysql # rm -rf /usr/lib64/mysql /usr/share/mysql [root@lo ...
- c#---Socean.Rpc之EasyProxy
目录 1.高性能RPC框架:Socean.RPC 2.Socean.RPC框架实测 3.Socean.Rpc之EasyProxy 简介 这几天给Socean.RPC加上了动态代理,简称EasyProx ...
- input禁止输入后,触发事件,在苹果手机的页面会滚动
在vue中,<input type="text" readonly="readonly" @click=""/>,点击跳转页面. ...
- ggEditor给节点增加提示框
参考官方文档: https://www.yuque.com/antv/g6/plugin.tool.tooltip 在react-ggEditor使用方法: import React from 're ...
- 浅析设计模式之mvc、mvp、mvvm
mvc.mvvm.mvp是常见的设计模式,也是常见的设计思想,现对它们进行简要的归纳总结 三种模式的介绍 1.MVC:经典设计模式 View 传送指令到 Controller(控制器) Control ...
- 电信IOT平台固件升级
1 离线签名 注意事项:特别重要,被坑了好久 A 将差分文件.bin格式的压缩成.zip 再进行签名 B 不能再中文目录下 否则,会出现校验失败 记住私钥 2 上传公钥 3 上传固件包 4 ...