Created
December 26, 2017 01:35
-
-
Save sourabh2k15/07964a7951d0e04dea9281dac3b64ca3 to your computer and use it in GitHub Desktop.
Leetcode #98 Validate if Tree is BST
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
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