算法:通过递归并限定上下限元素的值的范围可以计算
package com.bupt.acm.leetcode; public class ValidBinary { private class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int x){ val=x; } } public boolean isValidBST(TreeNode root){ if(root==null){ return true; } return validBST(root,Integer.MIN_VALUE,Integer.MAX_VALUE); } private boolean validBST(TreeNode root, int min, int max) { // TODO Auto-generated method stub if(root==null){ return true; } if(root.val>min&&root.val<max){ return validBST(root.left,min,root.val)&&validBST(root.right, root.val, max); }else{ return false; } } }