二叉树的遍历

来源:互联网 发布:阿里云个人邮箱登录 编辑:程序博客网 时间:2024/06/03 23:46
#include <stdio.h>#include <string.h>#include<iostream>#include<cmath>using namespace std;typedef long long LL;string a,b;void pre_vis(int a_begin,int a_end,int b_begin,int b_end){    if(a_begin > a_end) return ;    int i;    char root = a[a_begin];    for(i = b_begin; b[i] != root; i++);    pre_vis(a_begin + 1, a_end - b_end + i,b_begin,i-1);    pre_vis(a_end - b_end + i +1, a_end, i+1,b_end);    cout<<root;}void las_vis(int a_begin,int a_end,int b_begin,int b_end){    if(a_begin > a_end) return ;    int i ;    char root = a[a_end];    cout<<root;    for(i = b_begin; b[i] != root; i++);    las_vis(a_begin, a_end - b_end + i -1,b_begin, i-1);    las_vis(a_end - b_end +i, a_end -1, i + 1,b_end);}int main(){    while(cin>>a>>b)    {        int len_a = a.size();        int len_b = b.size();        //pre_vis(0,len_a-1,0,len_b-1);        las_vis(0,len_a-1,0,len_b-1);    }    return 0;}


0 0