SOJ 1310. Right-Heavy Tree
来源:互联网 发布:淘宝产品首页营销图片 编辑:程序博客网 时间:2024/05/01 19:49
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.
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.
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
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:
#include<iostream>#include<memory.h>using namespace std;struct BT{struct RHT{ int data; RHT *left; RHT *right; RHT(int a,RHT *left=NULL, RHT *RHT=NULL):data(a),left(left),right(right) {} };RHT *root;BT(){root=NULL;}void insert(const int & dat ){return ins(root,dat);}void ins(RHT * & root,const int & dat){ if(root != NULL) { if(root->data>=dat) ins(root->left,dat); else ins(root->right,dat); } else root = new RHT(dat);//for pointer's ,you must new operator}void Inorder(RHT * &root){ if(root!=NULL) { Inorder(root->left); cout<<" "<<root->data; Inorder(root->right); }}void Preorder(RHT * &root){ if(root!=NULL) { cout<<" "<<root->data; Preorder(root->left); Preorder(root->right); }}void Postorder(RHT * &root){ if(root!=NULL) { Postorder(root->left); Postorder(root->right); cout<<" "<<root->data; }}};int main(){ int n; bool first=true; int temp=0; while(cin>>n) { if(!first) cout<<endl; first=false; BT bt; for (int i = 0; i < n; ++i) { cin>>temp; bt.insert(temp); } cout<<"Inorder:"; bt.Inorder(bt.root); cout<<endl; cout<<"Preorder:"; bt.Preorder(bt.root); cout<<endl; cout<<"Postorder:"; bt.Postorder(bt.root); cout<<endl; } return 0;},这一个版本用了0.79sec,之前写了一个版本的,可能是因为没有按地址传递,所以TLE了。构造树和前序中序后序遍历其实挺容易的。
- SOJ 1310. Right-Heavy Tree
- 1310. Right-Heavy Tree
- 1310. Right-Heavy Tree
- Sicily 1310. Right-Heavy Tree
- Sicily 1310. Right-Heavy Tree
- sicily 1310. Right-Heavy Tree
- Right-heavy tree——笛卡尔树
- soj
- Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- leetocde_Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- Binary Tree Right Side View
- VC 快捷启动桌面制作
- 一天游戏后的感悟
- 写在Aras Innovator技术贴 前面
- UVA 11404 Palindromic Subsequence
- 深入学习Django源码基础1 - 深刻理解Python中的元类(metaclass)
- SOJ 1310. Right-Heavy Tree
- 中国集成电路发展探秘
- MemSQL学习笔记-类似MySQL的数据库
- [LeetCode] Minimum Window Substring
- 黑马程序员--Java语法
- Javass高级理论听课笔记
- 将Emacs配置成一个C/C++的IDE编程环境
- Java 通过Web Services访问Aras Innovator PLM
- ADO访问数据库