Created
August 8, 2015 20:17
-
-
Save bragboy/163485522248cce287d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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