题目1184:二叉树遍历

来源:互联网 发布:手工香皂洗脸好吗 知乎 编辑:程序博客网 时间:2024/05/29 10:34
#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;//1184char 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;//1184char 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****************************************************************/

0 0
原创粉丝点击