二叉树三种序列的两种转化

来源:互联网 发布:淘宝网天猫女手提包 编辑:程序博客网 时间:2024/06/17 03:45

已经大四了。保研完成,是时候应该给自己放松一下!但是心里久久不能平静,因为找工作的人讨论的东西我会的并不多。真的现在出去找工作不知道会找到什么样的,所以说还是好好读研吧!最近发现自己的数据结构——算法+语法已经烂到极致,ccf200分,真是一年不如一年,我也不能再骗自己了,拿起数据结构+算法要从头再学一遍。。

百度三面提到了前序遍历,中序遍历以及后序遍历的相互转化,自己学习学习并练习一下!

#include<iostream>#include<string>#include<stdlib.h>using namespace std;string get_last_order(string first_order,string middle_order){    if(first_order.length()!=middle_order.length())        cout<<"Failed!"<<endl;    if(first_order.length()==0)    {        return string();    }    if(first_order.length()==1)    {        if(first_order[0]!=middle_order[0])            cout<<"Failed!"<<endl;    }    char flag = first_order[0];    int where = middle_order.find(flag);    if( where == string::npos )    {        cout << "Failed!" <<endl;        exit(1);    }    int m=first_order.length();    int n=middle_order.length();    string left_tree = get_last_order(first_order.substr(1,where),middle_order.substr(0,where));    string right_tree = get_last_order(first_order.substr(where+1,m-1),middle_order.substr(where+1,n-1));    return left_tree + right_tree + flag;}int main(){    string first_order,middle_order;    cout<<"前序遍历:";    cin>>first_order;    cout<<"中序遍历:";    cin>>middle_order;    cout<<"后序遍历:"<<get_last_order(first_order,middle_order)<<endl;}

0 0