Fork me on GitHub


题目1184:二叉树遍历
时间限制:1 秒内存限制:32 兆特殊判题:否提交:1503解决:598
题目描述:
编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。
例如如下的先序遍历字符串:
ABC##DE#G##F###
其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。
输入:
输入包括1行字符串,长度不超过100。
输出:
可能有多组测试数据,对于每组数据,
输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。
每个输出结果占一行。
样例输入:
abc##de#g##f###
样例输出:
c b e g d f a 
来源:
2002年华中科技大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7907-1-1.html

 


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char s[105];
int tag;
typedef struct node
{
    char data;
    struct node *left,*right;
}Node;
void mid(Node *p)
{
    if(p)
    {
        mid(p->left);
        printf("%c ",p->data);
        mid(p->right);
    }
}
Node *contree()
{
    Node *p;
    char c;
    c=s[tag++];
    if(c=='#'||tag>strlen(s))
    {
        return NULL;
    }
    else
    {
        p=(Node *)malloc(sizeof(Node));
        p->data=c;
        p->left=contree();
        p->right=contree();
         
    }
    return p;
}
int main()
{
    Node *head=NULL;
    while(scanf("%s",s)!=EOF)
    {
        tag=0;
        head=contree();
        mid(head);
        printf("\n");
 
    }
}
/**************************************************************
    Problem: 1184
    User: vs_kunkka
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:904 kb
****************************************************************/
#include<stdio.h>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include <map>
#include <stack>
class node
{
public :node *lchild,*rchild;char c;
};
node first;
using namespace std;
stack<int> st;
void inorder(node *a)
{
    if(a->lchild!=&first&&a->lchild!=NULL) inorder(a->lchild);
    if(a!=NULL) printf("%c ",a->c);
    if(a->rchild!=&first&&a->rchild!=NULL) inorder(a->rchild);

}
int main()
{    
//    freopen("in.txt","r",stdin);first.c='c';
    int i,j,len;char s[101];node a[101];
    while(~scanf("%s",s) )
    { 
        len=strlen(s);
        while(!st.empty()) st.pop();
        a[0].c=s[0];
        a[0].lchild=&first;
        a[0].rchild=&first;
        st.push(0);
        for(i=1;i<len;i++)
        {
            //    printf("%c\n",a[st.top()].c);
            //    inorder(&a[0]);
            //    printf("\n");
            if(s[i]!='#') 
            {
                a[i].c=s[i];
                a[i].lchild=&first;a[i].rchild=&first;
                while(1)
                {
                    if(a[st.top()].lchild==&first) 
                    {a[st.top()].lchild=&a[i];
                    break;
                    }
                    else if(a[st.top()].rchild==&first) 
                    {a[st.top()].rchild=&a[i];break;}
                    else if(a[st.top()].rchild!=&first)
                         st.pop();
                    //break;
                }
                st.push(i);
            }
            else 
            {
                while(1)
                {
                if(a[st.top()].lchild==&first)
                {a[st.top()].lchild=NULL;break;}
                else if(a[st.top()].rchild==&first)
                {a[st.top()].rchild=NULL;break;}
                else 
                    st.pop();
                //    break;
                }
            }
        }
        inorder(&a[0]);
        printf("\n");
    }
    return 0;
}

 

posted on 2013-02-24 20:35  huashiyiqike  阅读(315)  评论(0编辑  收藏  举报