Skip to content

Latest commit

 

History

History
31 lines (28 loc) · 673 Bytes

File metadata and controls

31 lines (28 loc) · 673 Bytes

Leetcode 98:验证二分搜索树

思路1:递归判断

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
 const helper = (root,lower,upper) => {
  if(!root){
    return true;
  }
  if(root.val<=lower || root.val>=upper){
    return false;
  }
  return helper(root.left,lower,root.val) && helper(root.right,root.val,upper);
}

var isValidBST = function(root) {
  return helper(root,-Infinity,Infinity)
};