Open Tcdian opened 4 years ago
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
let prevVal = -Infinity;
let result = true;
inOrderTravel(root);
return result;
function inOrderTravel(root) {
if (!result || root === null) {
return;
}
inOrderTravel(root.left);
if (prevVal >= root.val) {
result = false;
return;
}
prevVal = root.val;
inOrderTravel(root.right);
}
};
98. Validate Binary Search Tree
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
Example 1
Example 2