1102 Invert a Binary Tree (25 分)

#include<iostream>
#include<cstring>

using namespace std;

const int N = 20;

struct node{
    int l, r, p;
}tr[N];
int n;

int q[N], tt = -1, hh;

void trev(int u){
    if(u == -1) return;
    trev(tr[u].r);
    cout << u << ‘ ‘;
    trev(tr[u].l);
}

int main(){
    memset(tr, -1, sizeof tr);
    
    cin >> n;
    for(int i = 0; i < n; i ++){
        char a, b;
        cin >> a >> b;
        if(a != ‘-‘) tr[i].l = a - ‘0‘, tr[a - ‘0‘].p = i;
        if(b != ‘-‘) tr[i].r = b - ‘0‘, tr[b - ‘0‘].p = i;
    }
    
    int r = 0;
    for(int i = 0; i < n; i ++) 
        if(tr[i].p == -1){
            r = i;
            break;
        }
        
    q[++ tt] = r;
    while(hh <= tt){
        int a = q[hh ++];
        if(~tr[a].r) q[++ tt] = tr[a].r;
        if(~tr[a].l) q[++ tt] = tr[a].l;
    }
    
    for(int i = 0; i <= tt; i ++) cout << q[i] << ‘ ‘;
    cout << endl;
    
    trev(r);
    
    return 0;
}

1102 Invert a Binary Tree (25 分)

上一篇:Django之ModelForm


下一篇:Worksheet对象 应用示例