0.7 不知道是不是因为left写成了right
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x){ val = x; } } public class Main{ public static boolean check(TreeNode root){ if(root == null) return true; List<Integer> res = new ArrayList<>(); inOrder(root, res); for(int i = 0; i < res.size()-1; i++){ if(res.get(i) > res.get(i+1)) return false; } return true; } private static void inOrder(TreeNode root,List<Integer> list){ if(root == null) return ; inOrder(root.left, list); list.add(root.val); inOrder(root.right, list); } public static void main(String[] args){ List<Integer> res = new ArrayList<>(); Scanner cin = new Scanner(System.in); String val = cin.nextLine(); String[] nums = val.split(","); for (int i = 0; i < nums.length; i++) { int v = Integer.parseInt(nums[i]); res.add(v); } TreeNode root = build(res, 0); if(check(root) == false) System.out.println("False"); else System.out.println("True"); } private static TreeNode build(List<Integer> a, int index) { TreeNode t = null; if(index < a.size()) { Integer val = a.get(index); if(val == null) { return null; } t = new TreeNode(val); t.left = build(a, 2*index+1); t.right = build(a, 2*index+2); return t; } return t; } }