题解 | 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")

全部评论

相关推荐

面向对象的火龙果很爱...:去吃一顿炸鸡就走
点赞 评论 收藏
分享
07-09 18:33
门头沟学院 Java
这么逆天每年都有人去???&nbsp;填多益网申就是大型的服从性测试
鲁大牛:辅导员在群里发了这个公司我就申了一下。网申居然要写当场开摄像头写两篇不少于三百字的作文。太逆天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务