题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
https://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
/*class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
export function isValidBST(root: TreeNode): boolean {
// write code here
return isValidBST1(root,null,null)
}
function isValidBST1(root:TreeNode,min,max):boolean{
if(root === null)return true
if(min !== null && root.val <= min.val){
return false
}
if(max !== null && root.val >= max.val){
return false
}
return isValidBST1(root.left, min, root)
&& isValidBST1(root.right, root, max);
}


