[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 ...
随机推荐
- 【C++】随机数引擎
rand() 基本:使用随机数时,经常见到的是C标准库提供的函数rand(),这个函数会生成一个0到RAND_MAX之间的一个整形数: 分布:为了得到一个给定范围内的随机数,通常会对生成的随机数取余: ...
- Linux学习2-云服务器上安装java和tomcat环境
在linux上部署java的项目,首先要安装JDK和Tomcat,具体要求怎么操作呢,我们一起来学习吧! JDK的安装步骤如下: 1.首先我们从官网下载jdk-8u231-linux-x64.rpm安 ...
- Request库的安装与使用
Request库的安装与使用 安装 pip install reqeusts Requests库的7个主要使用方法 requests.request() 构造一个请求,支撑以下各方法的基础方法 req ...
- 解决Python2.7的UnicodeEncodeError: 'ascii' codec can't encode异常错误
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128) ...
- django的自定义权限
最近在写发布系统,涉及到权限的控制 参考 黄小墨同学的博客实现了 如下 1:定义一张权限控制的表 [root@localhost app01]# tailf -25 models.py class P ...
- Linux 软件安装卸载 (源码、rpm)
Linux下软件的安装主要有两种不同的形式.第一种安装为源码安装,文件名为xxx.tar.gz压缩包为主;以第一种方式发行的软件多为以源码形式发送的.第二种方式则是另一种安装文件名为xxx.i386. ...
- H5浏览器强制手机横屏
H5强制手机横屏 1. 通过screen.orientation可以定义手机屏幕的方向,但是lock()方法仅在浏览器已经通过requestFullscreen()切换到全屏模式时起作用,例:强制手机 ...
- 大数相加-----杭电acm1002
#include<stdio.h> #include<string.h> int main() { ], ch2[]; ], num2[]; ; scanf("%d& ...
- Pyinstaller打包exe,丢失图标等问题
Pyinstaller打包exe,丢失图标等问题 一.原因 exe运行时会解压一个名为'_MEI*'的资源文件夹到电脑的临时目录,程序结束时删除. 程序里使用'\图标.png'这样的路径,exe运行时 ...
- css的三种导入方式
内联样式表 <p style="font-size:50px; color:blue">css内联样式表</p> 内部样式表 <style type= ...