Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Created December 26, 2017 01:35
Show Gist options
  • Save sourabh2k15/07964a7951d0e04dea9281dac3b64ca3 to your computer and use it in GitHub Desktop.
Save sourabh2k15/07964a7951d0e04dea9281dac3b64ca3 to your computer and use it in GitHub Desktop.
Leetcode #98 Validate if Tree is BST
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
stack<TreeNode*> s;
TreeNode* prev = NULL;
while(root != NULL || !s.empty()){
while(root != NULL){
s.push(root);
root = root->left;
}
root = s.top(); s.pop();
if(prev != NULL && prev->val >= root->val) return false;
prev = root;
root = root->right;
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment