Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public boolean isValidBST(TreeNode root) {
  12.  
  13. if(root == null) return true;
  14.  
  15. if(root.right == null && root.left == null){
  16. return true;
  17. }
  18.  
  19. return isValidHelper(root, Double.MAX_VALUE, Double.NEGATIVE_INFINITY);
  20. }
  21.  
  22. public boolean isValidHelper(TreeNode root, double max, double min){
  23.  
  24. if(root == null) return true;
  25.  
  26. if(root.val <= min || root.val >= max){
  27. return false;
  28. }
  29.  
  30. return isValidHelper(root.left, root.val, min) && isValidHelper(root.right, max, root.val);
  31.  
  32. }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement