Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Iterative inorder traversal solution:
- bool isValidBST(TreeNode* root) {
- stack<TreeNode*> to_process;
- TreeNode* curr = root;
- TreeNode* min_sofar = nullptr;
- while(curr || !to_process.empty()) {
- while (curr) {
- to_process.push(curr);
- curr = curr->left;
- }
- curr = to_process.top();
- to_process.pop();
- if (min_sofar && min_sofar->val >= curr->val) return false;
- min_sofar = curr;
- curr = curr->right;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement