平时学习都是核心代码模式,现在是acm模式,需要自己写
Solution solution = new Solution();
↓已经通过结果
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Solution solution = new Solution();
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
TreeNode[ ] TreeNodes = new TreeNode[n+1];
for(int i=1;i<=n;i++){
TreeNodes[i] = new TreeNode(0);
}
for(int i =1;i<=n;i++){
TreeNodes[i].val = in.nextInt();
TreeNodes[i].left = TreeNodes[in.nextInt()];
TreeNodes[i].right = TreeNodes[in.nextInt()];
}
boolean flag = solution.isValidBST(TreeNodes[m]);
System.out.println(flag);
}
}
class TreeNode{
TreeNode left;
TreeNode right;
int val;
public TreeNode(int val){
this.val = val;
this.left = null;
this.right = null;
}
}
class Solution {
long pre = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root==null)return true;
//访问左子树
if(!isValidBST(root.left)){
return false;
}
if(root.val <= pre){
return false; //一开始左子树大于Long中的最小值
}
pre = root.val;
//访问右子树
if(!isValidBST(root.right)){
return false;
}
return true;
}
}

京公网安备 11010502036488号