Skip to content

Instantly share code, notes, and snippets.

@bragboy
Created August 8, 2015 20:17
Show Gist options
  • Select an option

  • Save bragboy/163485522248cce287d3 to your computer and use it in GitHub Desktop.

Select an option

Save bragboy/163485522248cce287d3 to your computer and use it in GitHub Desktop.
package dsa.tree;
public class Tree {
Node root;
public static void main(String args[]) {
Tree anyTree = new Tree();
anyTree.root = new Node(3);
anyTree.root.left = new Node(2);
anyTree.root.right = new Node(5);
anyTree.root.left.left = new Node(1);
anyTree.root.left.right = new Node(4);
if(anyTree.isBst(anyTree.root)){
System.out.println("Is BST");
}else{
System.out.println("Is Not bst");
}
}
private int MINVALUE = -1;
public boolean isBst(Node node) {
if(node.left!=null){
if(!isBst(node.left)) return false;
}
if(node.data > MINVALUE){
MINVALUE = node.data;
}else{
return false;
}
if(node.right!=null){
if(!isBst(node.right)) return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment