Skip to content

Instantly share code, notes, and snippets.

@dineshrajpurohit
Created September 5, 2014 20:25
Show Gist options
  • Save dineshrajpurohit/eaea667ef7723ccd8041 to your computer and use it in GitHub Desktop.
Save dineshrajpurohit/eaea667ef7723ccd8041 to your computer and use it in GitHub Desktop.
BSTValidity
public Boolean isValidBinary(BinaryTreeNode root, BinaryTreeNode prev){
// Stack 3 2 1
// 1 2 3
// if(current.left != null ){
// Boolean left = isValidBinary(current.left);
// if(left == false)
// return false;
// else{
// if(root.right.item > root.item){
// return isValidBinary(root.right);
// }else if(root.
// }
// }
/**
* I thought of storing the previous value of the node
**/
if(root == null)
return true;
else if(root.left == null && root.right==null)
return true;
else if(!isValidBinary(root.left, prev))
return false;
else if(prev != null && (int)root.item <= (int)prev.item)
return false;
prev = root;
return isValidBinary(root.right, prev);
// 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment