P1827

p1827https://www.luogu.com.cn/problem/P1827
思路
前序遍历的的第一个一定是根节点。在中序遍历中找到该节点,可将中序遍历分成左子树和右子树,重复这个过程,过程中也将后序输出。

代码

#include <bits/stdc++.h>
using namespace std;
string pre,inor;
void use(string pre,string inor){
	if(pre.empty()) return;
	char root=pre[0];
	int k=inor.find(root);
	pre.erase(pre.begin());
	use(pre.substr(0,k),inor.substr(0,k));
	use(pre.substr(k),inor.substr(k+1));
	printf("%c",root);
}
int main(){
	cin>>inor>>pre;
	use(pre,inor);
	
	return 0;
}

日志
6.10发布 伪代码
6.11写出代码
posted @ 2025-06-10 21:51  虚拟星辰  阅读(0)  评论(0)    收藏  举报