hiho第十周——后序遍历

来源:互联网 发布:软件架构重要性 编辑:程序博客网 时间:2024/06/05 21:13

hiho第十周——后序遍历

题目链接:http://hihocoder.com/problemset/problem/1049

注意点

string a = "1";string b = a.substr(0,0);string c = a.substr(0,1);string d = a.substr(1,0);string e = a.substr(1,1);

运行不会出错,结果是c=“1”其余都是空字符串。

可执行代码

#include<iostream>#include<fstream>#include<string>using namespace std;typedef struct Node{    char data;    Node *left,*right;}Node;//后序遍历string houxu(Node * root){    string res;    if(root==NULL)        return res;    string left = houxu(root->left);    string right = houxu(root->right);    return left+right+root->data;}Node* CreateTree(string pre,string mid){    int len = pre.size();    if(len==0)        return NULL;    int locate = mid.find(pre[0]);    int left_len = locate;    int right_len = len-locate-1;    Node * node = new Node;    node->data = pre[0];    node->left = CreateTree(pre.substr(1,left_len),mid.substr(0,left_len));    node->right = CreateTree(pre.substr(1+left_len,right_len),mid.substr(locate+1,right_len));    return node;}int main(){    //ifstream cin("input.txt");    string pre,mid;    cin>>pre>>mid;    Node *root = new Node;    root = CreateTree(pre,mid);    string ans = houxu(root);    cout<<ans;    return 0;}
0 0