九度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
****************************************************************/



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值