题目1184:二叉树遍历

#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <stack>
#include <string>
#include <string.h>
#include <stdio.h>
#include <cmath>
#include <map>
#include <functional>
#include <set>
#include <math.h>
using namespace std;
//1184
char pre[101];
int loc,cur;
struct t{
    t *l,*r;
    char c;
}T[101];
t *creat(){
    T[loc].r=T[loc].r=NULL;
    return &T[loc++];
}
t *build(int len){
    if(cur==len)    return NULL;
    t *root;
    if(pre[cur++]=='#') return NULL;
    else{
        root=creat();
        root->c=pre[cur-1];
        root->l=build(len);
        root->r=build(len);
    }
    return root;
}
void inOrder(t *root){
    if(root->l)  inOrder(root->l);
    printf("%c ",root->c);
    if(root->r)  inOrder(root->r);
}
int main(){
    //freopen("input.txt","r",stdin);
    while(scanf("%s",pre)!=EOF){
        int l=strlen(pre);
        loc=0,cur=0;
        t *root=build(l);
        inOrder(root);
        puts("");
    }
    return 0;
}
/**************************************************************
    Problem: 1184
    User: cust123
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
//1184
char pre[101];
int i,len;
bool f;
struct t{
    t* l;
    t* r;
    char d;
};
t* build(int len){
    if(i==len) return NULL;
    t* root;
    if(pre[i++]=='#')   root=NULL;
    else{
        root=(t*)malloc(sizeof(t));
        root->d=pre[i-1];
        root->l=build(len);
        root->r=build(len);
    }
    return root;
}
void inOrder(t* root){
    if(root->l) inOrder(root->l);
    printf("%c ",root->d);
    if(root->r) inOrder(root->r);
}
int main(){
   // freopen("input.txt","r",stdin);
    while(scanf("%s",pre)!=EOF){
        len=strlen(pre);
        i=0;
        t* root=build(len);
        f=true;
        inOrder(root);
        cout<<endl;
    }
    return 0;
}
/**************************************************************
    Problem: 1184
    User: cust123
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:1520 kb
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值