poj2255

it2022-05-09  21

 1 #include <stdio.h>  2 #include <stdlib.h>  3   4  char a1[ 30],a2[ 30];  5  int c, l1,l2;  6  void Tree( int i, int j)  7 {  8      int k;  9      char root; 10      if (i > j) 11      return ; 12     root = a1[c++]; 13      for(k=i;k<=j;k++) 14          if(root == a2[k]) 15          break; 16     Tree(i,k- 1); 17     Tree(k+ 1,j); 18     printf( " %c ",root); 19  20 } 21  int main() 22 { 23      while(scanf( " %s %s ",a1,a2)!=EOF) 24     { 25         getchar(); 26         l2 = strlen(a2); 27         c =  0; 28         Tree( 0,l2- 1); 29         printf( " \n "); 30  31     } 32  33      return  0;

34 }其实针对这种题,说白了,自己还是无法定义树,这个东西,自己还是不熟练,练习的太少了,自己的多练习,针对这道题,不用多想必须是递归,然后利用中序和先序的根来寻找,然后不断递归,就行了

 

转载于:https://www.cnblogs.com/yelcoved/archive/2013/02/09/2909682.html

相关资源:数据结构—成绩单生成器

最新回复(0)