zoukankan      html  css  js  c++  java
  • 九度oj 题目1078:二叉树遍历

    题目1078:二叉树遍历

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:5326

    解决:3174

    题目描述:

    二叉树的前序、中序、后序遍历的定义:
    前序遍历:对任一子树,先访问跟,然后遍历其左子树,最后遍历其右子树;
    中序遍历:对任一子树,先遍历其左子树,然后访问根,最后遍历其右子树;
    后序遍历:对任一子树,先遍历其左子树,然后遍历其右子树,最后访问根。
    给定一棵二叉树的前序遍历和中序遍历,求其后序遍历(提示:给定前序遍历与中序遍历能够唯一确定后序遍历)。

    输入:

    两个字符串,其长度n均小于等于26。
    第一行为前序遍历,第二行为中序遍历。
    二叉树中的结点名称以大写字母表示:A,B,C....最多26个结点。

    输出:

    输入样例可能有多组,对于每组测试样例,
    输出一行,为后序遍历的字符串。

    样例输入:
    ABC
    BAC
    FDXEAG
    XDEFAG
    样例输出:
    BCA
    XEDGAF

    分析:先递归重建二叉树,在递归后序遍历。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include <string>
     5 using namespace std;
     6 
     7 typedef struct Node{
     8   char data;
     9   struct Node *lchild;
    10   struct Node *rchild;
    11 }BTree;
    12 
    13 string str1, str2;
    14 void postOrder(BTree *T)
    15 {
    16  if(T!=NULL)
    17  {if(T->lchild!=NULL) 
    18     postOrder(T->lchild);
    19   if(T->rchild!=NULL)
    20      postOrder(T->rchild);
    21   cout<<T->data;
    22  }
    23  return;
    24 }
    25 
    26 BTree *trans(int l1,int h1,int l2,int h2){ 
    27       if(l1 > h1 || l2 > h2)//递归出口,不符合条件,直接返回空节点 
    28             return NULL;
    29     int j = l2;
    30     
    31     while(str2[j] != str1[l1]){ j++; }
    32     
    33     //构造根节点 
    34     BTree *T=(BTree*)malloc(sizeof(BTree));  
    35     T->data=str2[j];   
    36     T->lchild=NULL;   
    37     T->rchild=NULL;
    38     
    39     T->lchild=trans(l1+1,l1+j-l2,l2,j-1); //返回左子树 
    40     T->rchild=trans(l1+j-l2+1,h1,j+1,h2); //返回右子树 
    41     return T;
    42 }
    43 
    44 
    45 int main()
    46 {
    47        
    48     while(cin >> str1 >> str2)
    49     {
    50       int L1 = str1.length();
    51       int L2 = str2.length();
    52       BTree *T = trans(0,L1-1,0,L2-1);
    53       postOrder(T);
    54       cout << endl;
    55     }
    56   return 0;
    57 }
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 typedef struct Node{
     8   char data;
     9   struct Node *lchild;
    10   struct Node *rchild;
    11 }BTree;
    12 
    13 char str1[30], str2[30];
    14 
    15 void postOrder(BTree *T){
    16      if(T != NULL){
    17         if(T->lchild != NULL) 
    18             postOrder(T->lchild);
    19           if(T->rchild != NULL)
    20              postOrder(T->rchild);
    21           printf("%c", T->data);
    22      }
    23      return;
    24 }
    25 
    26 BTree *trans(int l1,int h1,int l2,int h2){ 
    27     if(l1 > h1 || l2 > h2)//递归出口,不符合条件,直接返回空节点 
    28             return NULL;
    29     int j = l2;
    30     
    31     while(str2[j] != str1[l1]){ j++; }
    32     
    33     //构造根节点 
    34     BTree *T = (BTree*)malloc(sizeof(BTree));  
    35     T->data = str2[j];   
    36     T->lchild = NULL;   
    37     T->rchild = NULL;
    38     
    39     T->lchild = trans(l1+1, l1+j-l2, l2, j-1); //返回左子树 
    40     T->rchild = trans(l1+j-l2+1, h1, j+1, h2); //返回右子树 
    41     return T;
    42 }
    43 
    44 
    45 int main()
    46 {
    47        int L1, L2;
    48     while(scanf("%s %s", str1, str2) != EOF){
    49       L1 = strlen(str1);
    50       L2 = strlen(str2);
    51       BTree *T = trans(0,L1-1,0,L2-1);
    52       postOrder(T);
    53       printf("
    ");
    54     }
    55   return 0;
    56 }
     
  • 相关阅读:
    使用_Capistrano_进行自动化部署(2)
    使用 Capistrano 进行自动化部署
    Zend_Framework_1 框架是如何被启动的?
    PHP新版本变化
    Phpstorm 无法自动断点 Exception
    理解希尔排序
    C# Thread 线程
    Unity 依赖注入容器的AOP扩展
    C# 面向切面编程 AOP
    C# 表达式树 Expression
  • 原文地址:https://www.cnblogs.com/qinduanyinghua/p/6418728.html
Copyright © 2011-2022 走看看