题目链接:LeeCode98验证二叉搜索树
题目描述:
中序遍历一遍,保证数组严格升序就行
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private static List<Integer> list;
public static boolean isValidBST(TreeNode root) {
list=new ArrayList<>();
isture(root);
for (int i = 1; i < list.size(); i++) {
if(list.get(i-1)>=list.get(i)){
return false;
}
}
return true;
}
public static void isture(TreeNode node){
if(node==null)return;
isture(node.left);
list.add(node.val);
isture(node.right);
}
}