• 重建二叉树Tree Recovery


    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!
    Input

    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.
    Output
    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 Input

    DBACEGF ABCDEFG
    BCAD CBAD
    Sample Output
    ACBFGED
    CDAB

     1 #include <stdio.h>//程序来自DiaoCow
     2 #include <stdlib.h>
     3 #include <string.h>
     4 
     5 typedef struct Node
     6 {
     7     char    chValue;
     8     struct Node    *lChild;
     9     struct Node    *rChild;
    10 }Node;
    11 
    12 //重建二叉树
    13 void Rebuild(char *pPreOrder , char *pInOrder , Node **pRoot , int nTreeLen)
    14 {
    15     int  nLeftLen , nRightLen;
    16     char *pLeftEnd;
    17     Node *p;
    18 
    19     //边界条件检查
    20     if(!pPreOrder || !pInOrder || !pRoot)    return;   
    21 
    22     if(!(p = (Node *)malloc(sizeof(Node))))    return;
    23     p->chValue = *pPreOrder;    
    24     p->lChild = p->rChild = NULL;
    25     *pRoot = p;
    26     
    27     if(nTreeLen == 1)    return;
    28 
    29     //划分左右子树
    30     pLeftEnd = pInOrder;
    31     while(*pLeftEnd != *pPreOrder)    pLeftEnd++;//在中序序列中找到根的位置,根的左右分别是左右子树
    32 nLeftLen = (int)(pLeftEnd - pInOrder);//计算左右子树序列的长度 33 nRightLen = nTreeLen - nLeftLen - 1; 34 35 if(nLeftLen) Rebuild(pPreOrder + 1 , pInOrder , &(p->lChild) , nLeftLen); 36 if(nRightLen) Rebuild(pPreOrder + nLeftLen + 1, pInOrder + nLeftLen + 1 , &(p->rChild) , nRightLen); 37 } 38 39 //后序遍历 40 void PostOrder(Node *p) 41 { 42 if(p) 43 { 44 PostOrder(p->lChild); 45 PostOrder(p->rChild); 46 printf("%c",p->chValue); 47 } 48 } 49 50 int main(void) 51 { 52 char PreOrder[32] , InOrder[32]; 53 Node *pTree; 54 printf("依次输入先序和中序序列:"); 55 //输入先序和中序序列 56 while(scanf("%s%s", PreOrder , InOrder) != EOF) 57 { 58 Rebuild(PreOrder , InOrder , &pTree , strlen(PreOrder)); 59 PostOrder(pTree); 60 printf("\n"); 61 } 62 return 0; 63 }
  • 相关阅读:
    PHP编译安装
    PHP编译安装
    Apache编译安装
    Apache编译安装
    端口号
    端口号
    初步理解TCP/IP网络
    初步理解TCP/IP网络
    剑指offer——树的子结构
    STL四种智能指针
  • 原文地址:https://www.cnblogs.com/richardcpp/p/2718640.html
Copyright © 2020-2023  润新知