[ACM] 【Binary Tree】 Traversal problem

Line cutting

Note: Continuous update

Knowing the order, ordering, and ordering

Luogu 1827 American origin

Code:

#include<bits/stdc++.h>
using namespace std;
void dfs(string xx,string zx){
	if(!xx.size()) return;
	int pos=zx.find(xx[0]);
	dfs(xx.substr(1,pos),zx.substr(0,pos));
	dfs(xx.substr(pos+1),zx.substr(pos+1));
	printf("%c",xx[0]);
}
int main(){
	string xx,zx;
	cin>>zx>>xx;
	dfs(xx,zx);
	printf("\n");
}

Find the preorder in the postorder

Luogu 1030 Seeking order

Code:

#include<bits/stdc++.h>
using namespace std;
void dfs(string zx,string hx){
	if(!zx.size())return;
	int pos=zx.find(hx[hx.size()-1]);
	printf("%c",zx[pos]);
	dfs(zx.substr(0,pos),hx.substr(0,pos));
	dfs(zx.substr(pos+1),hx.substr(pos,hx.size()-pos-1));
} 
int main(){
	string zx,hx;
	cin>>zx>>hx;
	dfs(zx,hx);
	printf("\n");
}

Find the number of mid-orders, given the order first

Luogu 1229 traversal problem

Code:

Idea: Obviously, if there is only one son, there will be a middle-order disagreement. To find the number of nodes with only one son, you only need to find how many pairs of letters are just next to each other in the preorder and postorder and exchange each other. For example, ab in the preamble and ba in the postamble. Obviously, a has only one son b.

#include<bits/stdc++.h>
using namespace std;
int main(){
	char a[400],b[400];
	scanf("%s%s",a,b);
	long long n=0;
	for(int i=0;i<strlen(a)-1;i++)
		for(int j=1;j<strlen(b);j++)
			if(b[j]==a[i]&&a[i+1]==b[j-1]){
				n++;continue;
			}
	printf("%lld\n",1<<n);
}
Published 9 original articles · won 0 · 99 visits

Guess you like

Origin blog.csdn.net/weixin_45497996/article/details/105450279