博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Aizu - ALDS1_7_C Tree Walk 树的三种遍历
阅读量:3904 次
发布时间:2019-05-23

本文共 2546 字,大约阅读时间需要 8 分钟。

Binary trees are defined recursively. A binary tree T is a structure defined on a finite set of nodes that either

  • contains no nodes, or
  • is composed of three disjoint sets of nodes:
    - a root node.
    - a binary tree called its left subtree.
    - a binary tree called its right subtree.

Your task is to write a program which perform tree walks (systematically traverse all nodes in a tree) based on the following algorithms:

  1. Print the root, the left subtree and right subtree (preorder).
  2. Print the left subtree, the root and right subtree (inorder).
  3. Print the left subtree, right subtree and the root (postorder).

Here, the given binary tree consists of n nodes and evey node has a unique ID from 0 to n-1.

Input

The first line of the input includes an integer n, the number of nodes of the tree.

In the next n linen, the information of each node is given in the following format:

id left right

id is the node ID, left is ID of the left child and right is ID of the right child. If the node does not have the left (right) child, the left(right) is indicated by -1

Output

In the 1st line, print "Preorder", and in the 2nd line print a list of node IDs obtained by the preorder tree walk.

In the 3rd line, print "Inorder", and in the 4th line print a list of node IDs obtained by the inorder tree walk.

In the 5th line, print "Postorder", and in the 6th line print a list of node IDs obtained by the postorder tree walk.

Print a space character before each node ID.

Constraints

  • 1 ≤ n ≤ 25

Sample Input 1

90 1 41 2 32 -1 -13 -1 -14 5 85 6 76 -1 -17 -1 -18 -1 -1

Sample Output 1

Preorder 0 1 2 3 4 5 6 7 8Inorder 2 1 3 0 6 5 7 4 8Postorder 2 3 1 6 7 5 8 4 0

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

 字符串数组记得要开大。。。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;const int maxn=30;int n;struct tree{ int parent; int left,right;};tree node[maxn];vector
ve[3];void init (){ memset (node,-1,sizeof(node));}void Ftraverse(int x){ if(x==-1) return; ve[0].push_back(x); Ftraverse (node[x].left); Ftraverse (node[x].right);}void Mtraverse(int x){ if(x==-1) return; Mtraverse (node[x].left); ve[1].push_back(x); Mtraverse (node[x].right);}void Etraverse(int x){ if(x==-1) return; Etraverse (node[x].left); Etraverse (node[x].right); ve[2].push_back(x);}void print(){ char s[4][15]={"Preorder","Inorder","Postorder"}; for (int i=0;i<3;i++) { printf("%s\n",s[i]); for (int j=0;j

 

转载地址:http://gpaen.baihongyu.com/

你可能感兴趣的文章
openresty lua zlib整合安装 让lua支持解压服务端压缩过的数据
查看>>
Nginx与Gzip请求
查看>>
最佳日志实践(v2.0)
查看>>
logstash日志分析的配置和使用
查看>>
Nginx问题定位之监控进程异常退出
查看>>
https://imququ.com/post/content-encoding-header-in-http.html
查看>>
如何监控 Nginx?
查看>>
字符编码的前世今生
查看>>
视频笔记:Go 抓包、分析、注入 - John Leon
查看>>
matplotlib 画图
查看>>
linux下模拟丢包,延时命令总结
查看>>
java的字符流简单介绍
查看>>
初识java的xml
查看>>
通过DOM方式对xml文件进行解析
查看>>
哈希桶处理哈希冲突
查看>>
位图(BitMap)&& 布隆过滤器(BloomFilter)
查看>>
总结: 笔试中常见virtual函数问题
查看>>
vue中使用mock模拟后端数据
查看>>
常见的数据库有哪几种?
查看>>
Java后端的SQL语句
查看>>