@@ -0,0 +1,2101 @@
+package com.sunny.grokkingalgorithms.fasttrack.treesandgraphs;%0A%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.BinaryTreeNode;%0Aimport com.sunny.grokkingalgorithms.ctci.c4.util.TreeUtils;%0A%0A/**%0A * Created by sundas on 6/9/2018.%0A */%0Apublic class CheckIfATreeIsABSTOrNot %7B%0A%0A  /*%0A  Check if a given binary tree is a BST or not.%0A   */%0A%0A  /**%0A   *%0A   * @param root%0A   * @return%0A   */%0A  public static boolean isBSTBruteForce(BinaryTreeNode root)%7B%0A    /*%0A    Empty tree is a BST%0A     */%0A    if(root == null)%7B%0A      return true;%0A    %7D%0A    int maxOfLeftSubTree = findMax(root.left);%0A    int minOfRightSubTree = findMin(root.right);%0A    if(root.data %3E= maxOfLeftSubTree%0A        && root.data %3C minOfRightSubTree)%7B%0A      /*%0A      Check if subtrees are bst or not, bST condition for this node has been validated.The BST condition should hold true%0A       for every node in the tree.%0A       */%0A      return (isBSTBruteForce(root.left) && isBSTBruteForce(root.right));%0A    %7D%0A    /*%0A    Failed BST condistion so false,no need to check further.%0A     */%0A    return false;%0A  %7D%0A%0A  /**%0A   *%0A   * @param root%0A   * @return%0A   */%0A  public static int findMax(BinaryTreeNode root)%7B%0A    if(root == null)%7B%0A      return Integer.MIN_VALUE;%0A    %7D%0A    int left = findMax(root.left);%0A    int right = findMax(root.right);%0A    int max = Math.max(left,right);%0A    max = Math.max(root.data,max);%0A    return max;%0A  %7D%0A%0A  /**%0A   *%0A   * @param root%0A   * @return%0A   */%0A  public static int findMin(BinaryTreeNode root)%7B%0A    if(root == null)%7B%0A      return Integer.MAX_VALUE;%0A    %7D%0A    int left = findMin(root.left);%0A    int right = findMin(root.right);%0A    int min = Math.min(left,right);%0A    min = Math.min(root.data,min);%0A    return min;%0A  %7D%0A%0A%0A  /**%0A   *%0A   * @param args%0A   */%0A  public static void main(String%5B%5D args) %7B%0A    BinaryTreeNode root = TreeUtils.createBST();%0A    TreeUtils.performBfsLevelMarker(root);%0A    System.out.println(isBSTBruteForce(root));%0A    System.out.println(%22########%22);%0A    root = TreeUtils.createUnBalancedBinaryTree();%0A    TreeUtils.performBfsLevelMarker(root);%0A    System.out.println(isBSTBruteForce(root));%0A  %7D%0A%0A%7D%0A

Check if a tree is BST or not using brute force.O(Nsquared)