Validate Binary Search Tree

来源:互联网 发布:手机安全中心软件 编辑:程序博客网 时间:2024/06/07 14:12
/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isValidBST(TreeNode root) {        return isValidBSTHelper(root, Long.MIN_VALUE, Long.MAX_VALUE);             }        private boolean isValidBSTHelper(TreeNode root, long left, long right) {        if (root == null) {            return true;        }        if (root.val <= left || root.val >= right) {            return false;        }        return isValidBSTHelper(root.left, left, root.val) && isValidBSTHelper(root.right, root.val, right);    }}

0 0