这道题,验证二叉搜索树,我在提交的时候发现别的测试用例竟然可以被存到现有测试用例,串了
于是我就知道,全局的变量不仅在每个测试用例是存在的,而且,不同测试用例他依然存在!
所以我改成
//这么写是因为左边是最小的
var isValidBST = function (root) {
let pre = null;
var validTree= function (root) {
if (!root) return true;
let left = validTree(root.left)
if (pre != null && root.val <= pre.val) return false;
pre = root
let right = validTree(root.right)
return left && right
}
return validTree(root)
};