Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  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.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement