二叉树已经知道先序中序推后序
title: 二叉树已经知道先序中序推后序
categories:
- ICPC
tags:
- null
abbrlink: bd001d9e
date: 2024-03-29 00:00:00
https://www.acwing.com/problem/content/3601/
不断找新的先序的根节点,根据位置切割中序,根据中序左右子树大小反切割先序,找到左子树对应的先序中序,然后递归处理
#include<stdio.h>
#include<vector>
#include<map>
#include<algorithm>
#include <algorithm>
#include <iostream>
#include<string>
using namespace std;
#define ll long long
//# define int long long
#define ull unsigned long long
#define pii pair<int,int>
#define baoliu(x, y) cout << fixed << setprecision(y) << x
#define endl "\n"
#define debug1(x) cerr<<"x"<<" "
#define debug2(x) cerr<<"x"<<endl
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
int n, m;
int a[N];
void dfs(string pre,string mid){
//if(pre.size()==1)
if(pre.empty())return ;
int idx=mid.find(pre[0]);
//左子树
dfs(pre.substr(1,idx),mid.substr(0,idx));
//右子树
dfs(pre.substr(idx+1),mid.substr(idx+1));
cout<<pre[0];
}
void solve(){
string pre,mid;
cin>>pre>>mid;
dfs(pre,mid);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t;
//cin>>t;
t=1;
while (t--) {
solve();
}
return 0;
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 爱飞鱼的blog!