中序遍历 (边遍历边验证顺序性)
private TreeNode prev = null;
private boolean isBST = true;
public boolean isValidBST(TreeNode root) {
inorder(root);
return isBST;
}
private void inorder(TreeNode node) {
if (node == null) return;
inorder(node.left);
if (prev != null && node.val <= prev.val) {
// 不符合 BST 性质
isBST = false;
return;
}
prev = node;
inorder(node.right);
}