1310 Right-HeavyTree 0.44s 求0.02秒的实现,我知道这里一定有牛人!!!

1310. Right-Heavy Tree
 
 
Total:558Accepted:82Rating:
2.8/5.0(4 votes)
 
   
   
 
Time Limit: 1sec    Memory Limit:32MB
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
 Copy sample input to clipboard
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.
// source code of submission 827956, Zhongshan University Online Judge System
// source code of submission 827613, Zhongshan University Online Judge System
#include<stdio.h>
struct node;
typedef struct node Node;
typedef struct node *Tree;
struct node{
    int info;
    Tree Left;
    Tree Right;
} Nodes[200001];
static int used = 0;
void ini(Tree& T){
    T->Left = T->Right = NULL;
    T->info = 0;
}
inline void insert(Tree& T, int item){
    Tree last;Tree tmp = T;bool flag =0;
    while(tmp != NULL) 
    {
        last = tmp;
        if(item <= tmp->info)
        {
            flag = 0;
            tmp = tmp->Left;
        }
        else
        {
            flag = 1;
            tmp = tmp->Right;
        }
    }
        if(flag == 0)
        {
            last->Left = &Nodes[used++];
            ini(last->Left);
            last->Left->info = item; 
            return;
        }
        else
        {
            last->Right = &Nodes[used++];
            ini(last->Right);
            last->Right->info = item; 
            return;
        }
}
inline void Print(const Tree& T){
    printf(" %d",T->info);
}
inline void PrintIn(const Tree& T){
    
    if(T->Left != NULL)
        PrintIn(T->Left);
    Print(T);
    if(T->Right != NULL)
        PrintIn(T->Right);
    
}
inline void PrintPre(const Tree& T){
    
    Print(T);
    if(T->Left != NULL)
        PrintPre(T->Left);
    if(T->Right != NULL)
        PrintPre(T->Right);
}
inline void PrintPos(const Tree& T){
    
    if(T->Left != NULL)
        PrintPos(T->Left);
    if(T->Right != NULL)
        PrintPos(T->Right);
    Print(T);
}
int main(){
    int NodeNum;bool isfirst = true ;
    while(scanf("%d",&NodeNum) != EOF)
    {
        if(!isfirst)
        {
            printf("/n");
        }
        isfirst = false;
        if (NodeNum == 0)
        {
            printf("Inorder:/nPreorder:/nPostorder:/n");
            continue;
        }
        Tree Root = &Nodes[used++];
        ini(Root);
        int data;scanf("%d",&data);
        Root->info = data;NodeNum--;
        while(NodeNum--)
        {
            scanf("%d",&data);
            insert(Root,data);
        }
        printf("Inorder:");
        PrintIn(Root);
        printf("/n");
        printf("Preorder:");
        PrintPre(Root);
        printf("/n");
        printf("Postorder:");
        PrintPos(Root);
        printf("/n");
        used = 0;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值