当前位置:网站首页>3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)

3598. Binary tree traversal (Huazhong University of Science and Technology exam questions)

2022-08-10 22:41:00 Ray.C.L

在这里插入图片描述

思路:dfs模拟建树

代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

void dfs(string pre, string in){
    
    if(pre.empty()) return ;
    
    char root = pre[0];
    int k = in.find(root);
    dfs(pre.substr(1, k), in.substr(0, k));
    dfs(pre.substr(k + 1), in.substr(k + 1));
    cout << root ;
}
int main()
{
    
    string pre, in;
    while(cin >> pre >> in){
    
        dfs(pre, in);
        cout << endl;
    }
    return 0;
}
原网站

版权声明
本文为[Ray.C.L]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208102141538979.html