九度oj 题目1201:二叉排序树
来源:互联网 发布:windows任务管理器画图 编辑:程序博客网 时间:2024/05/18 17:57
- 题目描述:
输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。
- 输入:
输入第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。
- 输出:
可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
每种遍历结果输出一行。每行最后一个数据之后有一个空格。
- 样例输入:
51 6 5 9 8
- 样例输出:
1 6 5 9 8 1 5 6 8 9 5 8 9 6 1
#include<iostream>#include<string>using namespace std;struct Node { Node *lchild; Node *rchild; int c;}node[110];int loc; //静态数组使用元素个数 Node *creat() { node[loc].lchild = node[loc].rchild = NULL; return &node[loc++];}void postOrder(Node *T) { if (T->lchild != NULL) postOrder(T->lchild); if (T->rchild != NULL) postOrder(T->rchild); cout << T->c << " ";} void inOrder(Node *T) { if (T->lchild != NULL) inOrder(T->lchild); cout << T->c << " "; if (T->rchild != NULL) inOrder(T->rchild);} void preOrder(Node *T) { cout << T->c << " "; if (T->lchild != NULL) preOrder(T->lchild); if (T->rchild != NULL) preOrder(T->rchild); }Node *Insert(Node *T, int x) { if (T == NULL) { T = creat(); T->c = x; return T; } else if (x < T->c) T->lchild = Insert (T->lchild, x); else if (x > T->c) T->rchild = Insert (T->rchild, x); return T;} int main (){ int n; while (cin >> n) { loc = 0; Node *T = NULL; while (n--) { int x; cin >> x; T = Insert(T, x); } preOrder(T); cout << endl; inOrder(T); cout << endl; postOrder(T); cout << endl; } return 0;}
0 0
- 九度oj 题目1201:二叉排序树
- 九度OJ题目1201:二叉排序树
- 九度OJ题目1201:二叉排序树
- 【九度OJ】题目1201:二叉排序树 解题报告
- 题目1467:二叉排序树 九度OJ
- 九度oj 题目1467:二叉排序树
- 九度oj 1201:二叉排序树
- 九度OJ 1201 二叉排序树
- 九度OJ-1201-二叉排序树
- 九度OJ - 1201 - 二叉排序树
- 九度OJ-1201-二叉排序树
- 九度OJ 1201 二叉排序树
- 九度题目1201:二叉排序树
- 九度题目1201:二叉排序树
- 九度题目1201:二叉排序树
- 题目1201:二叉排序树 -九度
- 【九度】题目1201:二叉排序树
- 【九度OJ】题目1467:二叉排序树 解题报告
- flex垃圾回收机制
- 编写更好的jQuery代码的建议
- JBoss 系列九十: 红帽发布 JBoss BPM Suite 6 最终版本
- 误删/var/lib/dpk文件夹
- 圆角ImageView
- 九度oj 题目1201:二叉排序树
- 黑马程序员--ASP.NET基础学习笔记
- OpenGL FBO 对象
- IOS开发之----常用函数和常数
- java中的闭包和回调
- IDA*算法
- 接口和抽象类
- hashtable和hashmap
- forward和redirect的区别