daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Jan 19th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Iterative inorder traversal solution:
  2.  
  3.  bool isValidBST(TreeNode* root) {
  4.         stack<TreeNode*> to_process;
  5.         TreeNode* curr = root;
  6.         TreeNode* min_sofar = nullptr;
  7.        
  8.         while(curr || !to_process.empty()) {
  9.             while (curr) {
  10.                 to_process.push(curr);
  11.                 curr = curr->left;
  12.             }
  13.            
  14.             curr = to_process.top();
  15.             to_process.pop();
  16.            
  17.             if (min_sofar && min_sofar->val >= curr->val) return false;
  18.            
  19.             min_sofar = curr;
  20.             curr = curr->right;
  21.         }
  22.        
  23.         return true;
  24.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top