Created
June 16, 2017 21:46
-
-
Save evidanary/25ce553798424034876b6da55b5577da 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
# Check if a binary tree is valid | |
class Solution | |
def initialize() | |
@prev = nil | |
end | |
def is_valid_bst(root) | |
return true if root.nil? | |
left = is_valid_bst(root.left) | |
return false if (@prev && (@prev >= root.val )) | |
@prev = root.val | |
right = is_valid_bst(root.right) | |
left && right | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment