Simple solution to this question


  • 0
    N
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            stack<TreeNode*> s;
    		TreeNode*cur = root;
    		 
    		long last = LONG_MIN;
    		while (cur != NULL || !s.empty()) {
    			while (cur != NULL) {
    				s.push(cur);
    				cur = cur->left;
    			}
    			TreeNode*top = s.top();
    			if ( top->val <= last)
    				return false;
     			last = top->val;
    			s.pop();
    			cur = top->right;
    		}
    		return true;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.