> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-08-2022-98.md).

# 01/08/2022 98

class Solution {

&#x20;public boolean isValidBST(TreeNode root){&#x20;

&#x20; return helper(root, null, null);\
}

```
private boolean helper(TreeNode root, TreeNode low, TreeNode high){
    if (root == null) return true;
    
    if ((low != null && root.val <= low.val) || (high != null && root.val >= high.val)){
        return false;
    }
    
    return helper(root.left, low, root) && helper(root.right, root, high);
    
}
```

}

class Solution {&#x20;

public boolean isValidBST(TreeNode root) {&#x20;

//List result = new ArrayList<>();&#x20;

Stack stack = new Stack<>();&#x20;

Integer prev = null;&#x20;

if (root == null) return true;

```
    while(!stack.isEmpty() || root != null){
        while(root != null){
            stack.push(root);
            root = root.left;
        }
         root = stack.pop();
        // If next element in inorder traversal
        // is smaller than the previous one
        // that's not BST.
        if (prev != null && root.val <= prev) {
            return false;
        }   
        //result.add(root.val); inorder traverse
        prev = root.val;
        root = root.right;
    }
     return true;
}
```

}
