<笔记>快速排序的切分

二叉树遍历

http://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce

#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 30;
unordered_map<char, int> pos, l, r // pos记录中序遍历所在元素的位置;
string pre, in;
string post;
void dfs(char root){
    if(l.count(root)) dfs(l[root]);
    if(r.count(root)) dfs(r[root]);
    post += root;
}

char dfs(int il, int ir, int pl, int pr){
    char root = pre[pl];
    int k = pos[root];
    if(k > il) l[root] = dfs(il, k - 1, pl + 1, pl + 1 + k - 1 - il);
    if(k < ir) r[root] = dfs(k + 1, ir, pl + 1 + k - il, pr);
    return root;
}

int main(){
    while(cin>> pre >> in){
        pos.clear(), l.clear(), r.clear();
        int n = pre.size();
        for(int i = 0; i < in.size(); i ++ ) pos[in[i]] = i;
        char root = dfs(0, n - 1, 0, n - 1);
        post.clear();
        dfs(root);
        cout << post << endl;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:21
被夸真的超级开心,好可爱的姐姐
码农索隆:老色批们不用脑补了,我把金智妮的图找来了查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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