//leetcode submit region begin(Prohibit modification and deletion)/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ 思路:搜索树,左子树小于根节点,根据这一特点,右子树大于根节点 class Solution { public boolean isValidBST(TreeNode root) { return isValid(root,null,null); } boolean isValid(TreeNode root,TreeNode min,TreeNode max){ if(root==null){ return true ; } ///节点左子树只包含 小于 当前节点的数量。 if(min! if(min!=null && min.val>=root.val){ return false; } ///节点的右子树只包括 大于 当前节点的数量。 if(max!=null && max.val<=root.val){ return false; } return isValid(root.left,min,root)&&isValid(root.right,root,max); }}//leetcode submit region end(Prohibit modification and deletion)
不爱世间浮华,不写红尘纷扰