Created
November 15, 2013 01:37
-
-
Save shiva27/7477733 to your computer and use it in GitHub Desktop.
Check if a given Binary Tree is a Binary Search Tree
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
public static void main(String ... args) | |
{ | |
isBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE); | |
} | |
private boolean isBST(TreeNode node, int max, int min) | |
{ | |
boolean left = false; | |
boolean right = false; | |
if(node.left != null) | |
{ | |
if(node.left.value > node.value || node.left.value < min) | |
{ | |
return false; | |
} | |
left = isBST(node.left, node.value, min); | |
} | |
else | |
{ | |
left = true; | |
} | |
if(node.right != null) | |
{ | |
if(node.right.value < node.value || node.right.value > max) | |
{ | |
return false; | |
} | |
right = isBST(node.right, max, node.value); | |
} | |
else | |
{ | |
right = true; | |
} | |
return left && right; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment