二叉树已经知道先序中序推后序

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; }

__EOF__

本文作者爱飞鱼
本文链接https://www.cnblogs.com/mathiter/p/17924420.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   potential-star  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示