class Main{ private static BTNode root; public static class BTNode{ BTNode left; BTNode right; int val; BTNode(int value){ this.val = value; } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); while (sc.hasNextLine()){ String str = sc.nextLine(); BinaryTree.BTNode root=createTree(str); inorder(root); } } public static int i=0; public static BinaryTree.BTNode createTree(String str){ BinaryTree.BTNode root=null; if(str.charAt(i)!='#'){ root=new BinaryTree.BTNode(str.charAt(i)); i++; root.left=createTree(str); root.right=createTree(str); }else{ i++; } return root; } public static void inorder(BinaryTree.BTNode root){ if(root==null) return; inorder(root.left); System.out.println(root.val+" "); inorder(root.right); }}
根据前序遍历构建二叉树,用中序遍历打印
于 2024-04-16 21:16:33 首次发布