Last active
March 18, 2020 14:45
-
-
Save DKNY1201/c2a242b9072eaf1f1334243cacad1504 to your computer and use it in GitHub Desktop.
This file contains 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
// https://leetcode.com/problems/validate-binary-search-tree/ | |
// Javascript version for Validate BST Tree using iteration | |
var isValidBST = function(root) { | |
if (!root) return true; | |
const stack = []; | |
let prevNode = null; | |
while(root != null || stack.length > 0) { | |
while(root != null) { | |
stack.push(root); | |
root = root.left; | |
} | |
root = stack.pop(); | |
if (prevNode && prevNode.val >= root.val) return false; | |
prevNode = root; | |
root = root.right; | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment