Sicily 1310. Right-Heavy Tree
2013-09-07 20:17
295 查看
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 6 9 5 5 6 3 2 9 3 3 2 8 4 2 1 4 3 2 5 1 0
Sample Output
Inorder: 1 2 3 4 5 6 7 8 Preorder: 8 2 1 4 3 7 5 6 Postorder: 1 3 6 5 7 4 2 8 Inorder: 2 2 3 3 3 5 5 6 9 Preorder: 5 5 3 2 2 3 3 6 9 Postorder: 2 3 3 2 3 5 9 6 5 Inorder: 1 1 2 2 3 4 4 5 Preorder: 4 2 1 1 2 4 3 5 Postorder: 1 2 1 3 4 2 5 4 Inorder: Preorder: Postorder:
Hint
The number of vertexs is no more than 200000.题意:
给出N个权值,按顺序插入到一棵二叉树中。要求所有子树中,根节点的左子树的所有权值小于等于根节点的权值,右子树的所有权值大于根节点的权值。最后按中序、前序和后序遍历顺序输出各个节点的权值。按普通二叉树的结构进行插入即可。
注意调用的函数需要加内联关键字“inline”,不然出现TLE。
inline的作用在学程序设计的时候接触过,但从来没用过,具体用法可以看看这个http://xinklabi.iteye.com/blog/676313
学数据结构的时候,树一直没有学好,以前每次遇到关于树的题目都会绕过不看(T_T所以导致现在连基本的树构建都不好,躲角落面壁思过自我谴责一万遍。。。),这题是参考了网上的代码变思考变敲的,总算做出来了,嗯,接下来要好好练练树一类的题==
代码如下:
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
struct Tree
{
Tree* right;
Tree* left;
int data;
Tree(int d):data(d),right(NULL),left(NULL){}
};
Tree *root;
int n,val;
inline void insert(Tree *&r,int num)
{
if(r!=NULL)
{
if(r->data>=num)insert(r->left,num);
else if(r->data<num)insert(r->right,num);
}
else
r=new Tree(val);
}
inline void pre(Tree *r)
{
cout<<' '<<r->data;
if(r->left!=NULL)pre(r->left);
if(r->right!=NULL)pre(r->right);
}
inline void in(Tree *r)
{
if(r->left!=NULL)in(r->left);
cout<<' '<<r->data;
if(r->right!=NULL)in(r->right);
}
inline void post(Tree *r)
{
if(r->left!=NULL)post(r->left);
if(r->right)post(r->right);
cout<<' '<<r->data;
}
int main()
{
bool f=0;
while(cin>>n)
{
if(f)cout<<endl;
f=1;
root=NULL;
for(int i=0;i<n;i++)
{
cin>>val;
insert(root,val);
}
cout<<"Inorder:";
if(root!=NULL)in(root);
cout<<endl;
cout<<"Preorder:";
if(root!=NULL)pre(root);
cout<<endl;
cout<<"Postorder:";
if(root!=NULL)post(root);
cout<<endl;
}
return 0;
}
相关文章推荐
- Sicily 1310. Right-Heavy Tree
- sicily 1310. Right-Heavy Tree
- 1310. Right-Heavy Tree
- SOJ 1310. Right-Heavy Tree
- Sicily 1018 A Card Trick —— 教你玩一个纸牌魔术
- Sicily 1146 采药(DP动态规划——01背包问题)
- Sicily 1021 Couples(数据结构——栈)
- Sicily 1045. Space Management[离散化]
- Sicily 1800 Sequence(RMQ)
- Sicily 1802 Atomic Nucleus Investigation(线段树)
- Sicily 1934 移动小球
- 中山大学sicily第1813题
- Sicily 1210 二叉树 (Binary tree)
- sicily 1002
- sicily 2011
- sicily 1324
- sicily 1010
- sicily 1421 搬寝室
- Sicily 1149. 等价表达式
- sicily 1791