題解 | #判斷是不是二叉搜索樹#
判斷是不是二叉搜索樹
http://fangfengwang8.cn/practice/a69242b39baf45dea217815c7dedb52b
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代碼中的類名、方法名、參數(shù)名已經(jīng)指定,請勿修改,直接返回方法規(guī)定的值即可 * * * @param root TreeNode類 * @return bool布爾型 */ function isValidBST( root ) { // write code here console.log(root) if (root === null) { return true } if (!root.left && !root.right) { return true } let left = root.left === null? -Infinity : root.left.val let right = root.right === null? Infinity : root.right.val let left_right = root.left.right === null? -Infinity : root.left.right.val if ((left < root.val) && (root.val< right) && (left_right < root.val)) { return isValidBST(root.left) && isValidBST(root.right) } else { return false } } module.exports = { isValidBST : isValidBST };