码迷,mamicode.com
首页 > 其他好文 > 详细

URAL1136 Parliament(二叉树的遍历)

时间:2015-12-03 13:26:17      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

Description

A new parliament is elected in the state of MMMM. Each member of the parliament gets his unique positive integer identification number during the parliament registration. The numbers were given in a random order; gaps in the sequence of numbers were also possible. The chairs in the parliament were arranged resembling a tree-like structure. When members of the parliament entered the auditorium they took seats in the following order. The first of them took the chairman’s seat. Each of the following delegates headed left if his number was less than the chairman’s, or right, otherwise. After that he took the empty seat and declared himself as a wing chairman. If the seat of the wing chairman has been already taken then the seating algorithm continued in the same way: the delegate headed left or right depending on the wing chairman’s identification number.
The figure below demonstrates an example of the seating of the members of parliament if they entered the auditorium in the following order: 10, 5, 1, 7, 20, 25, 22, 21, 27.
技术分享
During its first session the parliament decided not to change the seats in the future. The speech order was also adopted. If the number of the session was odd then the members of parliament spoke in the following order: the left wing, the right wing and the chairman. If a wing had more than one parliamentarian then their speech order was the same: the left wing, the right wing, and the wing chairman. If the number of the session was even, the speech order was different: the right wing, the left wing, and the chairman. For a given example the speech order for odd sessions will be 1, 7, 5, 21, 22, 27, 25, 20, 10; while for even sessions — 27, 21, 22, 25, 20, 7, 1, 5, 10.
Determine the speech order for an even session if the speech order for an odd session is given.

Input

The first line of the input contains N, the total number of parliamentarians. The following lines contain N integer numbers, the identification numbers of the members of parliament according to the speech order for an odd session.
The total number of the members of parliament does not exceed 3000. Identification numbers do not exceed 65535.

Output

The output should contain the identification numbers of the members of parliament in accordance with the speech order for an even session.
 
题目分析:
该题目意思是给定一个odd sessions(左分支,右分支,根),求对应的even sessions(右分支,左分支,根)
前提条件是:左分支<右分支
所以思路就是先根据odd session建树,因为还存在 左分支<右分支 的条件,所以这个树是唯一的。
1)对于给定的odd session序列,建树规则:最后一个值肯定是根,前面比根小的都是左分支的,比根大的都是右分支的。
对于例子: 
N = 9 
数据:1 7 5 21 22 27 25 20 10
根据odd session的规则,所以10就是根节点。(1 7 5)是左子树:比10小
(21 22 27 25 20)是右子树:比10大
然后对于(1 7 5)再次建树,5是根节点,1是左子树,5是右子树
对所有的左右子树进行递归操作,即可建立一颗二叉树。

2)已经建好树了,对树按照even sessions(右分支,左分支,根)进行递归输出,即可。


代码如下:

#include <stdio.h>
#define MAX_NUMBER 3000
typedef struct nodeType
{
        int value;
        int left;
        int right;
}nodeType;

nodeType node[MAX_NUMBER];
int N;
int input[MAX_NUMBER];

int count = 0;

int buildTree(int begin, int end);
int printEven(int number);
int main()
{
        //freopen("input.txt","r",stdin);
        int i = 0;
        scanf("%d",&N);
        for(i=0;i<N;i++)
        {
                scanf(" %d",&input[i]);
         }
         buildTree(0,N-1);

         printEven(0);
         return 0;

}


int buildTree(int begin, int end)
{
         int i = 0;
         if(begin>end)   //如果只有右子树,不存在左子树,则会进入该条件。
            return -1;
         int pos = count;
         count++;

         node[pos].value = input[end];

         if(begin==end)
         {
                node[pos].left = -1;
                node[pos].right = -1;
                return pos;
         }

         for(i=begin;i<end;i++)
         {
               if(input[i]>input[end])
               {
                     break;
               }
         }
         int l = buildTree(begin,i-1);  //如果只有右子树,不存在左子树, begin 和 i相等,所以进入buildTree(),begin>end
         int r = buildTree(i,end-1);
         node[pos].left = l;
         node[pos].right = r;
         return pos;
}

int printEven(int number)
{
        if(number == -1)
             return 0;
        printEven(node[number].right);
        printEven(node[number].left);
        if(number==0)
             printf("%d\n",node[number].value);
        else
              printf("%d ",node[number].value);
        return 0;
}


URAL1136 Parliament(二叉树的遍历)

标签:

原文地址:http://www.cnblogs.com/xuxu-ning/p/5015520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!