当前位置:网站首页>P1087 FBI tree (DFS & binary tree)

P1087 FBI tree (DFS & binary tree)

2021-08-10 08:04:34 wx6110fa547fd20

P1087 FBI Trees (DFS& Binary tree )

  Subject portal

The question : Given 01 strand , According to the given (FBI) Define output postorder traversal .

Ideas : Binary tree traversal DFS The naked question of , Just write a conversion function .

#include<bits/stdc++.h>
using namespace std;
char jg(string a){// Conversion function  
	int x=0,y=0;
	for(int i=0;i<a.size();i++){
		if(a[i]=='0') x++;
		else  y++;
	}
	if(x&&y)  return 'F';
	if(x&&!y) return 'B';
	if(!x&&y) return 'I';
}
void dfs(string a){
	int l=a.size(); 
	if(l==1){ // The end condition . 
		cout<<jg(a);
		return;
	}
	dfs(a.substr(0,l>>1));// Satisfy the order of post order traversal . 
	dfs(a.substr(l>>1));
	cout<<jg(a);
}
int main(){
	int n,x=0,y=0;
	cin>>n;
	string a;
	cin>>a;
	dfs(a);
	puts("");
	return 0;
} 

      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

版权声明
本文为[wx6110fa547fd20]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210810080205571h.html

随机推荐