FBI树 java蓝桥杯
作者:互联网
import java.util.Scanner;
public class _2 {
static int n;
static String s;
static class treeNode{
char c;
treeNode left,right;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
s=sc.next();
treeNode root=new treeNode();
if(s.contains("0")&&s.contains("1"))root.c='F';
else if(s.contains("0"))root.c='B';
else root.c='I';
buildtree(s,root);
preorder(root);
}
static void buildtree(String s,treeNode root){
if(s.length()==1)return;
String s1=s.substring(0,s.length()/2);
String s2=s.substring(s.length()/2);
if(s1.contains("0")&&s1.contains("1")){
root.left=new treeNode();
root.left.c='F';
buildtree(s1,root.left);
}
else if(s1.contains("0")){
root.left=new treeNode();
root.left.c='B';
buildtree(s1,root.left);
}
else if(s1.contains("1")){
root.left=new treeNode();
root.left.c='I';
buildtree(s1,root.left);
}
if(s2.contains("0")&&s2.contains("1")){
root.right=new treeNode();
root.right.c='F';
buildtree(s2,root.right);
}
else if(s2.contains("0")){
root.right=new treeNode();
root.right.c='B';
buildtree(s2,root.right);
}
else if(s2.contains("1")){
root.right=new treeNode();
root.right.c='I';
buildtree(s2,root.right);
}
}
static void preorder(treeNode root){
if(root==null)return;
preorder(root.left);
preorder(root.right);
System.out.print(root.c);
}
}
标签:right,java,s2,contains,蓝桥,treeNode,FBI,root,left 来源: https://blog.csdn.net/tinkk/article/details/115025861