POJ 2255 Tree Recovery

Tree Recovery
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9879   Accepted: 6210

Descriptionnode

Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. 
This is an example of one of her creations: 

D
/ \
/ \
B E
/ \ \
/ \ \
A C G
/
/
F

To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG. 
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it). 

Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree. 
However, doing the reconstruction by hand, soon turned out to be tedious. 
So now she asks you to write a program that does the job for her! 

Inputios

The input will contain one or more test cases. 
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.) 
Input is terminated by end of file. 

Outputapi

For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

Sample Inputdom

DBACEGF ABCDEFG
BCAD CBAD

Sample Outputpost

ACBFGED
CDAB
題目大意:給出二叉樹的先序,中序序列,求二叉樹的後序序列。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <stack>
using namespace std;

typedef struct node
{
    char data;
    node *lchild;
    node *rchild;
    node()
    {
        lchild = rchild = NULL;
    }
}TreeNode;

//二叉樹的非遞歸後序遍歷
TreeNode *BuildTree(char *pre, char *in, int n)
{
    TreeNode *pNode;
    int k;
    char *p;
    if (n <= 0)
    {
        return NULL;
    }
    pNode = new TreeNode;//新建節點
    pNode->data = *pre;
    for (p = in; p < in + n; p++)//在中序序列中找等於*pre的位置k
    {
        if (*p == *pre)//pre指向根節點
        {
            break;//找到後退出,從而肯定根節點在in中的位置
        }
    }
    k = p - in;//左子樹的長度
    pNode->lchild = BuildTree(pre + 1, in, k);//遞歸構造左子樹
    pNode->rchild = BuildTree(pre + k + 1, p + 1, n - k - 1);//遞歸構造右子樹
    return pNode;//返回根節點
}

//二叉樹的非遞歸後序遍歷
void PostOrder(TreeNode *pRoot)
{
    stack<TreeNode *> Stack;
    TreeNode *p = pRoot;
    TreeNode *q;
    do 
    {
        while(p != NULL)
        {
            Stack.push(p);
            p = p->lchild;
        }
        q = NULL;
        while(!Stack.empty())
        {
            p = Stack.top();
            if (q == Stack.top()->rchild)
            {
                printf("%c", Stack.top()->data);
                q = Stack.top();
                Stack.pop();
            }
            else
            {
                p = p->rchild;
                break;
            }
        }
    } while (!Stack.empty());
}

//刪除二叉樹節點
void DeleteNode(TreeNode *pRoot)
{
    if (pRoot != NULL)
    {
        DeleteNode(pRoot->lchild);
        DeleteNode(pRoot->rchild);
    }
    delete pRoot;
}

int main()
{
    char pre[30], in[30];
    while(scanf("%s%s", pre, in) != EOF)
    {
        TreeNode *pRoot = BuildTree(pre, in, strlen(in));
        PostOrder(pRoot);
        printf("\n");
        DeleteNode(pRoot);
    }
    return 0;
}
相關文章
相關標籤/搜索