Sicily 3702. 二叉搜索树的遍历
来源:互联网 发布:matlab矩阵符号运算 编辑:程序博客网 时间:2024/06/08 11:26
二叉搜索树的遍历,老规矩,用数组来模拟(当然可以实际建一个二叉搜索树,其实也不会麻烦,只是要操作地址总感觉不太放心),然后递归进行前中序遍历。
Run Time: 0sec
Run Memory: 348KB
Code length: 1487Bytes
Submit Time: 2012-01-05 01:00:36
// Problem#: 3702// Submission#: 1174141// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <iostream>#include <cstring>using namespace std;struct Node { int data; int left, right;} node[ 3001 ];void preorder( int n ) { if ( n != 0 ) { cout << node[ n ].data << " "; preorder( node[ n ].left ); preorder( node[ n ].right ); }}void inorder( int n ) { if ( n != 0 ) { inorder( node[ n ].left ); cout << node[ n ].data << " "; inorder( node[ n ].right ); }}int main(){ int m; int i, j; while ( cin >> m && m ) { cin >> node[ 1 ].data; node[ 1 ].left = 0; node[ 1 ].right = 0; for ( i = 2; i <= m; i++ ) { cin >> node[ i ].data; node[ i ].left = 0; node[ i ].right = 0; j = 1; while ( j != 0 ) { if ( node[ i ].data < node[ j ].data ) { if ( node[ j ].left == 0 ) { node[ j ].left = i; j = 0; } else j = node[ j ].left; } else { if ( node[ j ].right == 0 ) { node[ j ].right = i; j = 0; } else j = node[ j ].right; } } } inorder( 1 ); cout << endl; preorder( 1 ); cout << endl; } return 0;}
- Sicily 3702. 二叉搜索树的遍历
- Sicily 1210 二叉树(树的遍历)
- 二叉搜索树的遍历
- 二叉搜索树的遍历
- 二叉搜索树的遍历
- sicily 1156.先序遍历二叉树
- 二叉搜索树的创建和遍历
- 二叉树的遍历以及搜索
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后序遍历
- 二叉搜索树的创建及其遍历
- 二叉搜索树的后序遍历
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后序遍历
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后续遍历序列
- 二叉搜索树的后序遍历
- 二叉搜索树的后续遍历序列
- linux命令行终端的翻屏滚屏more/less/head/tail
- Sicily 2503. 最长字符串
- jdbc增删改查
- richeidt 控件開發(二)
- 给定正整数 N, 求使N < 2^m 成立的最小m(C语言版)
- Sicily 3702. 二叉搜索树的遍历
- Sicily 3703. Huffman Coding V1
- 设计者的职责
- 计算Pi(1)
- Sicily 3825. AVL Tree
- jdbc防止sql注入学习记录
- LUA函数简介
- 使用内省的方式操作JavaBean
- 为launcher添加一个仿Mac的dock