Sicily 1310. Right-Heavy Tree
来源:互联网 发布:适马17 50 f2.8数据 编辑:程序博客网 时间:2024/05/22 11:38
1310. Right-Heavy Tree
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
A right-heavy tree is a binary tree where the value of a node is greater than or equal to the values of the nodes in its left subtree and less than the values of the nodes in its right subtree. A right-heavy tree could be empty.
Write a program that will create a right-heavy tree from a given input sequence and then traverse the tree and printing the value of the node each time a node is visited using inorder, preorder and postorder traversal.
The program should create the nodes in the tree dynamically. Thus, basically the tree can be of any size limited only by the amount of memory available in the computer.
Input
The input will contain several test cases, each of them as described below.
The first number in the input indicates the number of nodes in the tree. Then, the input is followed by the integers comprising the values of the nodes of the tree.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line. The output will be the sequence of node and labeled by the traversal method used, as shown in the sample output below.
Sample Input
8 8 2 4 7 5 3 1 69 5 5 6 3 2 9 3 3 28 4 2 1 4 3 2 5 10
Sample Output
Inorder: 1 2 3 4 5 6 7 8Preorder: 8 2 1 4 3 7 5 6Postorder: 1 3 6 5 7 4 2 8Inorder: 2 2 3 3 3 5 5 6 9Preorder: 5 5 3 2 2 3 3 6 9Postorder: 2 3 3 2 3 5 9 6 5Inorder: 1 1 2 2 3 4 4 5Preorder: 4 2 1 1 2 4 3 5Postorder: 1 2 1 3 4 2 5 4Inorder:Preorder:Postorder:
Hint
The number of vertexs is no more than 200000.
// Problem#: 1310// Submission#: 3298451// 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 <vector>#include <algorithm>#include <stdio.h>using namespace std;struct treeNode { int v; treeNode * l, * r; treeNode(int val = 0, treeNode * ll = NULL, treeNode * rr = NULL) { v = val; l = ll; r = rr; }};inline void insertT(treeNode * & now, int & val) { if (now == NULL) now = new treeNode(val); else if (val <= now->v) insertT(now->l, val); else insertT(now->r, val);}inline void inOrder(treeNode * & now) { if (!now) return; inOrder(now->l); printf(" %d", now->v); inOrder(now->r);}inline void preOrder(treeNode * & now) { if (!now) return; printf(" %d", now->v); preOrder(now->l); preOrder(now->r);}inline void postOrder(treeNode * & now) { if (!now) return; postOrder(now->l); postOrder(now->r); printf(" %d", now->v);}inline void deleteT(treeNode * & now) { if (!now) return; deleteT(now->l); deleteT(now->r); delete now;}int main() { //std::cout.sync_with_stdio(false); bool firstTime = true; int n; while (scanf("%d", &n) != EOF) { if (!firstTime) printf("\n"); treeNode * root = NULL; for (int i = 0; i < n; i++) { int temp; cin >> temp; insertT(root, temp); } printf("Inorder:"), inOrder(root); printf("\n"); printf("Preorder:"), preOrder(root); printf("\n"); printf("Postorder:"), postOrder(root); printf("\n"); deleteT(root); if (firstTime) firstTime = false; } return 0;}
- Sicily 1310. Right-Heavy Tree
- Sicily 1310. Right-Heavy Tree
- sicily 1310. Right-Heavy Tree
- 1310. Right-Heavy Tree
- 1310. Right-Heavy Tree
- SOJ 1310. Right-Heavy Tree
- Right-heavy tree——笛卡尔树
- sicily 1898 && 2608 Tree
- Sicily 1156 Binary tree
- Sicily 1156. Binary tree
- Sicily 3825. AVL Tree
- Sicily 1156. Binary tree
- sicily 1156.Binary tree
- Sicily 1898. Tree
- Sicily 2681. Tree
- Sicily 1875. Small tree
- sicily 1019. Apple Tree
- sicily 1875 Small tree
- Sicily 1336. Power Mean
- javascript中正则表达式和ruby中的一点差异
- 蓝桥杯算法训练之道路和航路 (邻接表SPFA+SLF优化)
- TPC-H
- PHP笔记--数据库操作
- Sicily 1310. Right-Heavy Tree
- Bootstrap-CSS-代码
- 所谓上拉电阻和下拉电阻
- Euler: Non-abundant sums
- Sicily 1276. Who needs 8 Queens w
- [UnityShader]material中的tiling与offset
- 实现位向量 1.6.2
- Sicily 1264. Atomic Car Race
- 数据库的事务隔离机制