import java.util.Scanner; class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(){ } public TreeNode(char val){ this.val=val; } } // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String tree=in.nextLine(); TreeNode root=prevOrder(tree); inOrder(root); } public static void inOrder(TreeNode root){ if(root==null)return; inOrder(root.left); System.out.print(root.val+" "); inOrder(root.right); } public static int i=0; public static TreeNode prevOrder(String s){ TreeNode root=new TreeNode(); char ch=s.charAt(i); if(ch!='#'){ root.val=ch; i++; }else{ i++; return null; } root.left=prevOrder(s); root.right=prevOrder(s); return root; } }