九度OJ1521

<pre name="code" class="cpp">#include<stdio.h>
#include<stdlib.h>
#include<iostream>
//其实整个过程中比较简单但其中输出是个问题,最后不能有空格,可以设置全局变量,也可以设置反回值。
typedef struct binarytree{
    int m_nValue;
    binarytree *m_pLeft;
    binarytree *m_pRight;
}BinaryTree;
 
int num;
 
void OutputRight(BinaryTree *Tree){
    if(Tree == NULL){
        return;
    }
     
    printf("%d", Tree->m_nValue);
    num--;
    if(num != 0){
        printf(" ");
    }
     
    OutputRight(Tree->m_pLeft);
    OutputRight(Tree->m_pRight);
 
}
void ReverseTree(BinaryTree *Tree){
    if(Tree == NULL){
        return ;
    }
    if(Tree->m_pLeft != NULL || Tree->m_pRight != NULL){
        BinaryTree *TmpTree = new BinaryTree();
        TmpTree = Tree->m_pLeft;
        Tree->m_pLeft = Tree->m_pRight;
        Tree->m_pRight = TmpTree;
             
        ReverseTree(Tree->m_pLeft);
        ReverseTree(Tree->m_pRight);
         
    }
    return ;
}
 
int main()
{
    int n;
    int i;
    char tmp;
    int data;
    int datal,datar; 
     
    BinaryTree *datainput[1002];
     
    while(scanf("%d", &n) != EOF){
 
        if(n <= 0){
            printf("NULL");
            continue;
        }else if(n == 1){
            getchar();
            scanf("%c",&tmp);
            if(tmp == 'z'){
                scanf("%d", &data);
                printf("%d",data);
                continue;
            }
             
        }
        num = n;
        for(i=0; i<n; i++){
            scanf("%d", &data);
            datainput[i] = new BinaryTree();
            datainput[i]->m_nValue = data;
        }
         
        for(i=0; i<n; i++){
            while((tmp = getchar()) != '\n');
            scanf("%c", &tmp);
            if(tmp == 'd'){
                scanf("%d %d", &datal, &datar);
                datainput[i]->m_pLeft = datainput[datal-1];
                datainput[i]->m_pRight = datainput[datar-1]; 
            }else if(tmp == 'l'){
                scanf("%d", &datal);
                datainput[i]->m_pLeft = datainput[datal-1];
                datainput[i]->m_pRight = NULL;
            }else if(tmp == 'r'){
                scanf("%d", &datar);
                datainput[i]->m_pLeft = NULL;
                datainput[i]->m_pRight = datainput[datar-1];
            }else if(tmp == 'z'){
                datainput[i]->m_pLeft = NULL;
                datainput[i]->m_pRight = NULL;
            }
        }
         
        ReverseTree(datainput[0]);
        OutputRight(datainput[0]);
 
    }
 
     
    return 0;
     
}
 
/**************************************************************
    Problem: 1521
    User: 星之河
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/


 

你可能感兴趣的:(九度)