题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
http://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
function isValidBST( root ) {
let max = -Infinity
function dfs( root ) {
if(root === null) return true
let v1 = dfs( root.left )
if(max >= root.val ) return false
max = max < root.val ? root.val : max
let v2 = dfs( root.right )
return v1 && v2
}
return dfs( root )
}
module.exports = {
isValidBST : isValidBST
};