Advertisement
sweet1cris

Untitled

Jan 9th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. // version 1 Traverse
  2. public class Solution {
  3.     private int lastVal = Integer.MIN_VALUE;
  4.     private boolean firstNode = true;
  5.     public boolean isValidBST(TreeNode root) {
  6.         if (root == null) {
  7.             return true;
  8.         }
  9.         if (!isValidBST(root.left)) {
  10.             return false;
  11.         }
  12.         if (!firstNode && lastVal >= root.val) {
  13.             return false;
  14.         }
  15.         firstNode = false;
  16.         lastVal = root.val;
  17.         if (!isValidBST(root.right)) {
  18.             return false;
  19.         }
  20.         return true;
  21.     }
  22. }
  23.  
  24.  
  25. // version 2  Divide and Conquer
  26. class ResultType {
  27.     boolean is_bst;
  28.     int maxValue, minValue;
  29.    
  30.     ResultType(boolean is_bst, int maxValue, int minValue) {
  31.         this.is_bst = is_bst;
  32.         this.maxValue = maxValue;
  33.         this.minValue = minValue;
  34.     }
  35. }
  36.  
  37. public class Solution {
  38.     /**
  39.      * @param root: The root of binary tree.
  40.      * @return: True if the binary tree is BST, or false
  41.      */
  42.     public boolean isValidBST(TreeNode root) {
  43.         ResultType r = validateHelper(root);
  44.         return r.is_bst;
  45.     }
  46.    
  47.     private ResultType validateHelper(TreeNode root) {
  48.         if (root == null) {
  49.             return new ResultType(true, Integer.MIN_VALUE, Integer.MAX_VALUE);
  50.         }
  51.        
  52.         ResultType left = validateHelper(root.left);
  53.         ResultType right = validateHelper(root.right);
  54.        
  55.         if (!left.is_bst || !right.is_bst) {
  56.             // if is_bst is false then minValue and maxValue are useless
  57.             return new ResultType(false, 0, 0);
  58.         }
  59.        
  60.         if (root.left != null && left.maxValue >= root.val ||
  61.               root.right != null && right.minValue <= root.val) {
  62.             return new ResultType(false, 0, 0);
  63.         }
  64.        
  65.         return new ResultType(true,
  66.                               Math.max(root.val, right.maxValue),
  67.                               Math.min(root.val, left.minValue));
  68.     }
  69. }
  70.  
  71. // version 3  Divide and Conquer
  72. public class Solution {
  73.     /**
  74.      * @param root: The root of binary tree.
  75.      * @return: True if the binary tree is BST, or false
  76.      */
  77.     public boolean isValidBST(TreeNode root) {
  78.         // write your code here
  79.         return divConq(root, Long.MIN_VALUE, Long.MAX_VALUE);
  80.     }
  81.    
  82.     private boolean divConq(TreeNode root, long min, long max){
  83.         if (root == null){
  84.             return true;
  85.         }
  86.         if (root.val <= min || root.val >= max){
  87.             return false;
  88.         }
  89.         return divConq(root.left, min, Math.min(max, root.val)) &&
  90.                 divConq(root.right, Math.max(min, root.val), max);
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement