文章

85

粉丝

0

获赞

480

访问

9.9k

头像
二叉树遍历2 题解:
P1401 华中科技大学/中国科学院机试题
发布于2026年3月6日 12:02
阅读数 172

#include <bits/stdc++.h>
using namespace std;
//前中序建树
string s = "";
void buildPost(string pre ,string in) {
    if (pre == "")
        return;
    char c = pre[0];
    int pos = in.find(c);
    string new_inleft = in.substr(0,pos);
    string new_inright = in.substr(pos+1);

    string new_preleft = pre.substr(1,new_inleft.length());
    string new_preright = pre.substr(1+new_inleft.length());

    buildPost(new_preleft,new_inleft);
    buildPost(new_preright,new_inright);
    s.push_back(c);

}

int main () {
    string pre = "";
    string in = "";
    while (cin >> pre>>in) {
        s="";
        buildPost(pre,in);
        cout<<s<<endl;
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发