#include<bits/stdc++.h> //#define int long long using namespace std; int back[]={5,2,6,4,1},mid[]={2,5,1,6,4},pre[]={1,2,5,4,6}; //string back,mid; void getback(int l,int r,int root){//通过前序和中序得到后序 if(l>r)return; int i = l; while (i<=r&&mid[i]!=back[root]) i++; getback(l,i-1,root+1); getback(i+1, r,root+1+i-l);//i-l为相对偏移量,思考的时候可以用整个树的根节点思考,当查找右子树的时候应该加上其左子树的节点个数 cout<<mid[i];//输出后序遍历结果 } void gethead(int l,int r,int root){//通过后序和中序得到前序 if(l>r)return; int i = l; while (i<=r&&mid[i]!=back[root]) i++; cout<<mid[i]; gethead(l,i-1,root-1-(r-i));//同理前序遍历,应该减去此时右子树的节点个数 gethead(i+1, r,root-1); } void solve(){ // getback(0,4,0); gethead(0,4,4);//从后往前查 } signed main() { //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int _ = 1; // cin >> _; while (_--) { solve(); } return 0; }