题解 | #判断是不是二叉搜索树#

判断是不是二叉搜索树

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);
}

全部评论

相关推荐

03-12 15:34
已编辑
北京邮电大学 Java
呓语0613:老哥你这黑马点评改造是在哪里看的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务