建二叉搜索树 import java.util.Scanner;; class TreeNode{ int val =0; TreeNode left=null; TreeNode right=null; TreeNode(int val){ this.val=val; } } public class Tree { //******************************************** public static TreeNode sortedTree(int[] nums){ TreeNode root = null; for(int num:nums) { if(root==null) { root = new TreeNode(num); continue; } helper(root,num); } return root; } public static TreeNode helper(TreeNode pNode,int num) { if(pNode==null) { return new TreeNode(num); } if(num<pNode.val) { pNode.left=helper(pNode.left,num); }else { pNode.right=helper(pNode.right, num); } return pNode; } //**************************************************** public static void mid(TreeNode node) { if(node!=null) { mid(node.left); System.out.println(node.val); mid(node.right); } } public static void main(String[] args) { Scanner in =new Scanner(System.in); String[] str = in.nextLine().trim().split(" "); int[] nums = new int[str.length]; int i=0; for(String s:str) { nums[i++]=Integer.parseInt(s); } TreeNode rNode = sortedTree(nums); mid(rNode); } }