zoukankan      html  css  js  c++  java
  • 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树:

    SDUT 1489

    Description

     已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历

    Input

     输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列。 

    Output

     输出二叉树的先序遍历序列

    Sample Input

    2
    dbgeafc
    dgebfca
    lnixu
    linux

    Sample Output

    abdegcf
    xnliu

    代码实现:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <vector>
     7 #include <map>
     8 #include<string.h>
     9 #include<stack>
    10 #include<set>
    11 #include <queue>
    12 using namespace std;
    13 struct Tree
    14 {
    15     char a;
    16     Tree * l;
    17     Tree *r;
    18 };
    19 //已知中序与后序递归建树::
    20 Tree * ipCreatTree(char *instar,char *inend ,char *poststar,char*postend)  //instar 中序首地址 inend 中序尾地址 poststar后序首地址 postend后序尾地址
    21 {
    22     Tree *root = (Tree*)malloc(sizeof(Tree));
    23 
    24     root->a = *postend;
    25 
    26     root->l = NULL;
    27 
    28     root->r = NULL;
    29 
    30     if(instar==inend&&poststar==postend) return root;
    31 
    32     char *inp = instar;
    33 
    34     while((*inp != root->a)&&(inend-inp>=0)) ++inp;
    35 
    36     int leftLength = inp - instar;
    37 
    38     char *leftInorderEnd = instar+leftLength-1;
    39 
    40     if(leftLength>0)
    41         root->l = ipCreatTree(instar,leftInorderEnd,poststar,poststar+leftLength-1);
    42     if(leftLength<inend-instar)
    43         root->r = ipCreatTree(leftInorderEnd+2,inend,poststar+leftLength,postend-1);
    44 
    45     return root;
    46 }
    47 void Preorder(Tree *r)
    48 {
    49     if(r == NULL)
    50         return;
    51     else
    52     {
    53         printf("%c",r->a);
    54         Preorder(r->l);
    55         Preorder(r->r);
    56     }
    57 }
    58 int main()
    59 {
    60     int t;
    61     char  inorder[51];
    62     char postorder[51];
    63     Tree  *root;
    64     scanf("%d",&t);
    65     getchar();
    66     while(t--)
    67     {
    68         scanf("%s%s",inorder,postorder);
    69         int inlen = strlen(inorder);
    70         int postlen = strlen(postorder);
    71         root=ipCreatTree(inorder,inorder+inlen-1,postorder,postorder+postlen-1);
    72         Preorder(root);
    73         printf("
    ");
    74     }
    75     return 0;
    76 }

    已知 先序&中序  建立二叉树:

    SDUT 3343

    Description

    给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。

    Input

    输入数据有多组,每组数据第一行输入 1 个正整数 N(1 <= N <= 50) 为树中结点总数,随后 2 行先后给出先序和中序遍历序列,均是长度为 N 的不包含重复英文字母 ( 区分大小写 ) 的字符串。

     

    Output

      输出一个整数,即该二叉树的高度。

    Sample Input

    9 
    ABDFGHIEC
    FDHGIBEAC

    Sample Output

    5
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <vector>
     7 #include <map>
     8 #include<string.h>
     9 #include<stack>
    10 #include<set>
    11 #include <queue>
    12 using namespace std;
    13 struct Tree
    14 {
    15     char a;
    16     Tree * l;
    17     Tree *r;
    18 };
    19 //已知先序与中序递归建树::
    20 Tree * piCreatTree(char *instar,char *inend ,char *prestar,char*preend) //instar 中序首地址 inend 中序尾地址 poststar先序首地址 postend先序尾地址
    21 {
    22     Tree *root = (Tree*)malloc(sizeof(Tree));
    23 
    24     root->a = *prestar;
    25 
    26     root->l = NULL;
    27 
    28     root->r = NULL;
    29 
    30     if(instar==inend&&prestar==preend) return root;
    31 
    32     char *inp = instar;
    33 
    34     while((*inp != root->a)&&(inend-inp>=0)) ++inp;
    35 
    36     int leftLength = inp - instar;
    37 
    38     char *leftInorderEnd = instar+leftLength-1;
    39 
    40     if(leftLength>0)
    41         root->l = piCreatTree(instar,leftInorderEnd,prestar+1,prestar+leftLength);
    42     if(leftLength<inend-instar)
    43         root->r = piCreatTree(leftInorderEnd+2,inend,prestar+leftLength+1,preend);
    44 
    45     return root;
    46 }
    47 
    48 int Depth(Tree *r)
    49 {
    50     int hl,hr;
    51     if(r==NULL) return 0 ;
    52     hl = Depth(r->l);
    53     hr = Depth(r->r);
    54     if(hl>=hr) return hl+1;
    55     else return hr+1;
    56 }
    57 int main()
    58 {
    59     int t;
    60     char  preorder[51];
    61     char   inorder[51];
    62     Tree  *root;
    63     while(~scanf("%d",&t))
    64     {
    65         getchar();
    66         scanf("%s%s",preorder,inorder);
    67         int inlen = strlen(inorder);
    68         int prelen = strlen(preorder);
    69         root=piCreatTree(inorder,inorder+inlen-1,preorder,preorder+prelen-1);
    70         printf("%d
    ",Depth(root));
    71     }
    72     return 0;
    73 }
    
    
    本文为个人随笔,如有不当之处,望各位大佬多多指教.
    若能为各位博友提供小小帮助,不胜荣幸.
     
  • 相关阅读:
    mysql常用技能分享
    php生成器使用总结
    MySQL索引使用方法和性能优化
    servlet相关
    UML图
    How Tomcat Works
    字符串编码
    高效工作
    php 设计模式总结
    python之装饰器
  • 原文地址:https://www.cnblogs.com/LGJC1314/p/7209581.html
Copyright © 2011-2022 走看看