Created
September 5, 2014 20:25
-
-
Save dineshrajpurohit/eaea667ef7723ccd8041 to your computer and use it in GitHub Desktop.
BSTValidity
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 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