Created
September 2, 2019 06:02
-
-
Save wenweixu/d84fac9a0fd2056d5abee889a647688e to your computer and use it in GitHub Desktop.
Hackerrank Is This a Binary Search Tree Python solution
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
| def checkBST(root): | |
| def is_BST(root): | |
| if root == None: | |
| return True, None, None | |
| bool_left, min_left, max_left = is_BST(root.left) | |
| bool_right, min_right, max_right = is_BST(root.right) | |
| if bool_left and bool_right: | |
| if root.left==None and root.right==None: | |
| return True, root.data, root.data | |
| elif root.left==None and not root.right==None: | |
| if min_right>root.data: | |
| return True, root.data, root.right | |
| elif not root.left==None and root.right==None: | |
| if max_left.data<root.data: | |
| return True, min_left, root.data | |
| elif not root.left==None and not root.right==None: | |
| if max_left<root.data and min_right>root.data: | |
| return True, min_left, max_right | |
| return False, None, None | |
| result, _, _ = is_BST(root) | |
| return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment