import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class TreeNode{ char val; TreeNode left; TreeNode right; public TreeNode(char val){ this.val = val; } } public static TreeNode buildTree(Queue<Character> input){ if (input == null){ return null; } char nodeVal = input.remove(); if (nodeVal == '#'){ return null; } TreeNode node = new TreeNode(nodeVal); node.left = buildTree(input); node.right = buildTree(input); return node; } public static void look(TreeNode root){ if (root == null){ return; } look(root.left); System.out.printf("%c ", root.val);; look(root.right); } public static void main(String[] args) { Scanner sca = new Scanner(System.in); while (sca.hasNextLine()){ String str = null; Queue<Character> input = new LinkedList<>(); if (sca.hasNext()){ str = sca.nextLine(); } if (str != null){ for (char ch : str.toCharArray()){ input.add(ch); } TreeNode root = buildTree(input); look(root); } } } }