Validate Binary Search Tree

来源:互联网 发布:淘宝店家身份查询 编辑:程序博客网 时间:2024/05/17 02:22
/** * 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 helper(root, Long.MIN_VALUE, Long.MAX_VALUE);    }        private boolean helper(TreeNode root, long left, long right) {        if (root == null) {            return true;        }        if (root.val > left && root.val < right) {            if (root.left != null && !helper(root.left, left, root.val)) {                return false;            }            if (root.right != null && !helper(root.right, root.val, right)) {                return false;            }            return true;        } else {            return false;        }    }}

0 0