【题目】

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的更多相关文章

  1. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  2. PAT A1020 Tree Traversals(25)

    题目描述 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

  3. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

  4. PAT 1020. Tree Traversals

    PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...

  5. PAT 1086 Tree Traversals Again[中序转后序][难]

    1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...

  6. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  7. PAT甲级——A1020 Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  8. A1020. Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  9. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

随机推荐

  1. Altium Designer打印多块PCB到热转印纸上、拼板发给工厂

    接下来介绍的方法的优势有: 节省电脑性能(来自不同PCB文件的图案被放置时只在第1块会卡顿一小会儿,之后不再卡顿) 便于排版(拖放图案时绝不会改变图案内容,拖放图案时鼠标拖住的矩形框的尺寸与图案的尺寸 ...

  2. O准备如何苟进复赛圈?华为软挑开挂指南(附赛题预测)

    事先声明,这不是华为软挑的软广,我也不是海军. 这篇文章纯粹是心血来潮,原因是去年上传到github的参赛代码,前几天又有两个人star和fork了. 记得star热潮还是去年4月复赛刚结束的那几天, ...

  3. vi/vim编辑器操作梳理

    1. vi/vim编辑器详细使用讲解 1.1 vi/vim编辑器的3种模式 1.2 vi/vim编辑器操作说明 参数/命令/模式 说明 ###普通模式   :set nu  显示行号 :set non ...

  4. 浅谈.NET中AppDomain的理解

    一.走进.NET AppDomain 天哪,.NET Framwork的CLR真是巧妙呢!随着越来越多的对.Net底层编程的了解,一些诸如架构,处理过程的复杂难懂的细节完全的让我叹服,所以呢,再次错过 ...

  5. 内部类(innerclasses)

    一般情况下,我们把类定义成独立的单元.有些情况下,我们把一个类放在另一个类的内部定义为内部类. 内部类的作用: 1.内部类提供了更好的封装.只能让外部类直接访问,不允许同一个包中的其他类直接访问. 2 ...

  6. Linux内存管理解析(三) : 内核对内核空间的内存管理

    内核采用 struct page 来表示一个物理页,在其中记载了诸多物理页的属性,比如 物理页被几个线程使用(如若没有则表示该页可以释放),页对应的虚拟地址. 首先需要知道的是,分配物理页可以分为两个 ...

  7. Shiro -- (四) 数据库支持

    主要就是JdbcRealm这个类 先看一下部分源码: 先建表:users(用户名 / 密码).user_roles(用户 / 角色).roles_permissions(角色 / 权限),并且往use ...

  8. GitLab Container Registry

    通过将GitLab Container Registry集成到GitLab中,每个项目都可以拥有自己的空间来存储其Docker镜像. 1. 启用Container Registry 如果在你的项目的侧 ...

  9. 基于JavaSwing开发银行信用卡管理系统

    开发环境: Windows操作系统开发工具: MyEclipse10/Eclipse+Jdk+Mysql数据库 运行效果图 源码及原文链接:https://javadao.xyz/forum.php? ...

  10. windows获取所有连接过的无线网密码

    一.打开命令行工具:win+R 输入cmd 回车进入: for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show pr ...