//注意,须自己在定义一个节点类,在main中实例化并调用 //注意,不要忘记static的使用 import java.util.Scanner; //节点类 class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(char val){ this.val=val; } } // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextLine()){ String str = sc.nextLine(); TreeNode root = createTree(str); inOrder(root); } } //创建子树 public static int i = 0; public static TreeNode createTree(String str){ TreeNode root = null; if (str.charAt(i) != '#'){ root = new TreeNode(str.charAt(i)); i++; root.left = createTree(str); root.right = createTree(str); }else { i++; } return root; } //中序遍历输出 public static void inOrder(TreeNode root){ //左右根 if(root!=null){ inOrder(root.left); System.out.print(root.val+" "); inOrder(root.right); } } }