题解 | Problem D

Problem D

https://www.nowcoder.com/practice/3769e5ca06594e959b4952c75a108aaf

#include <iostream>
using namespace std;

string toPostorder(string& preorder,string& inorder){
    if(preorder=="")return "";
    char root=preorder[0];
    int idx=inorder.find(root);
    string inorderleft=inorder.substr(0,idx);
    string inorderright=inorder.substr(idx+1);
    string preorderleft=preorder.substr(1,inorderleft.length());
    string preorderright=preorder.substr(inorderleft.length()+1);
    return toPostorder(preorderleft,inorderleft)+
    toPostorder(preorderright, inorderright)+root;
}

int main() {
    string preorder,inorder;
    while (cin >> preorder >> inorder) { // 注意 while 处理多个 case
        cout<<toPostorder(preorder,inorder)<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

明天不下雨了:兄弟你是我今天看到的最好看的简历(我说的是简历风格跟简历书写)把985 211再搞亮一点。投boss就说;您好,我华科(985)研二在读,本科211。对您的岗位很感兴趣,希望能获得一次投递机会。
点赞 评论 收藏
分享
华泰证券信息技术部 软开 月base2.2w,年终要看当年收益,HR讲往年应届平均35W
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务