[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 ...
随机推荐
- qt creator源码全方面分析(2-0)
目录 Extending Qt Creator Manual 生成领域特定的代码和模板 代码片段 文件和项目模板 自定义向导 支持其他文件类型 MIME类型 高亮和缩进 自定义文本编辑器 其他自定义编 ...
- Spark作业执行流程源码解析
目录 相关概念 概述 源码解析 作业提交 划分&提交调度阶段 提交任务 执行任务 结果处理 Reference 本文梳理一下Spark作业执行的流程. Spark作业和任务调度系统是其核心,通 ...
- VFP CursorAdapter 起步三(作者:Doug Hennig 译者:fbilo)
可重用数据类 VFP 的程序员们想要一个可重用的数据类已经很久了.尽管在过去的版本中也有许多解决这个问题的办法,不过总是有点美中不足.现在在 VFP 8里,我们有了真正的可重用数据类.这个月,Doug ...
- Java HashMap 四种遍历方式
HashMap遍历方式包含以下4种: 1.遍历KeySet,再通过Key来getValue. 2.使用entrySet的迭代器. 3.foreach entrySet的方式. 3.foreache v ...
- zabbix-server配置文件详解
zabbix官方文档:https://www.zabbix.com/documentation/4.0/zh/manual zabbix-server端的配置文件在/etc/zabbix/zabbix ...
- 将你的Archlinux打造成路由器
弄了一块J2900双千兆网口的工控板回来(奇怪的型号)当软路由用,无奈我又想各种皮,还想装桌面环境配VNC,而且我还对虚拟机不感冒(况且这U还不支持直通),只得放弃所有路由器系统.作为一名Arch铁粉 ...
- 浅析Internet上使用的安全协议
Internet上使用的安全协议 网络安全是分层实现的,从应用层安全到数据链路层安全. 一.运输层安全协议:安全套接字SSL 1.1.简介 SSL 是安全套接层 (Secure Socket Laye ...
- React之虚拟DOM中的Diff算法
一.React中的setState ( 异步函数,异步获取数据 ) 若操作的时间间隔短,它可以将多个setState结合成一个setState,减少虚拟DOM的比对次数,提高性能 二.同层虚拟DOM对 ...
- c++ bool
bool 就两个值,真或者假,通常用来存储关系表达式或者逻辑表达式的结果. 以前是用 int 来表示真假,大 int 有多个值,所以才规定 0 为假,非零为真,导致对应关系比较麻烦,有了 bool 就 ...
- bootstrap-table.js 自适应窗体设置
$(window).resize(function () { $('#dtOrder').bootstrapTable('resetView') })