Skip to content

Instantly share code, notes, and snippets.

@DKNY1201
Last active March 18, 2020 14:45
Show Gist options
  • Save DKNY1201/c2a242b9072eaf1f1334243cacad1504 to your computer and use it in GitHub Desktop.
Save DKNY1201/c2a242b9072eaf1f1334243cacad1504 to your computer and use it in GitHub Desktop.
// 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