hihocoder 1049 后續遍歷

#include<iostream>  
#include<cstdio>  
#include<string>  
#include<cstdlib>  
#include<string.h>  
using namespace std;  
  
  
char preorder[30],midorder[30],endorder[30];  
typedef struct mynode Node;  
typedef Node* Tree;  
  
  
struct mynode  
{  
    char data;  
    struct mynode* left;  
    struct mynode* right;  
};  
  
  
  
  
int build(int left,int right,Tree &root)//更新了一下使用引用使結構更加清晰  
{  
    if(left>right)  
        return 0;  
    int i,j,flag = 1;  
    for(i=0; i<strlen(preorder); i++)  
    {  
        for(j=left; j<=right; j++)  
            if(preorder[i]==midorder[j])  
            {  
                flag = 0;  
                break;  
            }  
        if(!flag)  
            break;  
    }  
    root = (Node*)malloc(sizeof(Node));  
    if(root==NULL)  
    {  
        perror("malloc failed");  
        exit(1);  
    }  
    root->left = NULL;  
    root->right = NULL;  
  
  
    root->data = preorder[i];  
    build(left,j-1,root->left);  
    build(j+1,right,root->right);  
}  
  
  
void del(Tree root)  
{  
    if(root!=NULL)  
    {  
        del(root->left);  
        del(root->right);  
    }  
    if(root!=NULL)  
    {  
        free(root);  
        root = NULL;  
    }  
}  
int pos = 0;  
int endcal(Tree root)  
{  
    if(pos == strlen(preorder))  
        return 0;  
    if(root->left)  
    {  
        endcal(root->left);  
  
  
    }  
    if(root->right)  
    {  
        endcal(root->right);  
    }  
    endorder[pos++] = root->data;  
}  
  
  
int main()  
{  
  
  
    Tree root;  
    cin>>preorder>>midorder;  
    build(0,strlen(preorder)-1,root);  
    endcal(root);  
    cout<<endorder<<endl;  
    del(root);  
}  

代碼可以AC不知道有沒有內存泄漏 有錯誤請指出

下面是別人寫的

#include<iostream>  
#include<cstdio>  
#include<cstdlib>  
#include<string.h>  
using namespace std;  
  
char preorder[30],inorder[30],endorder[30];  
  
void endcal(const char *pre,const char *in,int len)  
{  
    if(len<1)  
        return ;  
    int i=0;  
    while(in[i]!=pre[0])i++;  
    endcal(pre+1,in,i);  
    endcal(pre+i+1,in+i+1,len-i-1);  
    cout<<pre[0];  
}  
  
int main()  
{  
    cin>>preorder>>inorder;  
    endcal(preorder,inorder,strlen(preorder));  
}  

順便給出前序遍歷

#include<iostream>  
#include<cstdio>  
#include<cstdlib>  
#include<string.h>  
using namespace std;  
  
char pre[30],in[30],last[30];  
  
void precal(char *last,char *in,int lastend,int inend)  
{  
    if(lastend<0||inend<1)  
        return ;  
    int i=0;  
    while(in[i]!=last[lastend-1])i++;  
    cout<<last[lastend-1];  
    precal(last,in,lastend-inend+i,i);  
    precal(last,in+i+1,lastend-1,inend-i-1);  
}  
  
int main()  
{  
    while(cin>>last>>in)  
        precal(last,in,strlen(last),strlen(in)),putchar('\n');  
}  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,511評論 25 708
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 1 你知道嗎?在當下這個快節奏而又有壓力的環境中,我們的肉身與內心時常是分裂的。 我們每天面對的的事務越來越多,內...
    我們盛裝出行閱讀 684評論 0 1
  • 文/雨煉 《南宋的音樂》 (一) 樂音看看手機,在看看旁邊相談盛歡的兩人,咬咬唇,猶豫再三,還是決定厚著臉...
    雨煉閱讀 663評論 2 3
  • 今天先生騎小黃車接兒子放學,父子倆一起騎著小黃車直奔披薩店,然后與我視頻,聽著先生與孩子的對話,心里暖暖的,因為我...
    周志英閱讀 133評論 0 1