1 public class Solution { 2 public boolean isValidBST(TreeNode root) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(root == null) 6 return true; 7 if(root.left != null) 8 { 9 TreeNode left = root.left; 10 while(left.right != null) 11 left = left.right; 12 if(root.val <= left.val) 13 return false; 14 } 15 if(root.right != null) 16 { 17 TreeNode right = root.right; 18 while(right.left != null) 19 right = right.left; 20 if(root.val >= right.val) 21 return false; 22 } 23 return isValidBST(root.left) && isValidBST(root.right); 24 } 25 }