Problem1841--【树和二叉树】5-4 基于二叉链表的二叉树的双序遍历

1841: 【树和二叉树】5-4 基于二叉链表的二叉树的双序遍历

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1  Solved: 1
[Submit] [Status] [Web Board] [Creator:]

Description

设二叉树中每个结点的元素均为一个字符,按先序遍历的顺序建立二叉链表,编写递归算法实现该二叉树的双序遍历(双序遍历是指对于二叉树的每一个结点来说,先访问这个结点,再按双序遍历它的左子树,然后再一次访问这个结点,接下来按双序遍历它的右子树)。

Input

多组数据。每组数据一行,为二叉树的先序序列(序列中元素为‘0’时,表示该结点为空)。当输入只有一个“0”时,输入结束。

Output

每组数据输出一行,为双序遍历法得到的二叉树序列。

Sample Input Copy

ab000
ab00c00
0

Sample Output Copy

abba
abbacc

HINT

#include<iostream>
#include <string.h>
using namespace std;
typedef struct BiTNode
{
    char data;
    struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char S[],int &i)
{//先序建立二叉树
    if(S[i]=='0')
        T=NULL;
    else
    {
        T=new BiTNode;
        T->data=S[i];
        CreateBiTree(T->lchild,S,++i);
        CreateBiTree(T->rchild,S,++i);
    }
}
void DoubleTraverse(BiTree T)
{//双序遍历二叉树T的递归算法
/**************begin************/



    /**************end************/
}
int main()
{
    char S[100];
    while(cin>>S)
    {
        if(strcmp(S,"0")==0) break;
        int i=-1;
        BiTree T;
        CreateBiTree(T,S,++i);
        DoubleTraverse(T);
        cout<<endl;
    }
    return 0;
}

Source/Category