Problem

TC: O(n), SC: O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValid(root,-1001,1001);
    }
    // inorder dfs
    public boolean isValid(TreeNode root,int l, int r){
        if(root ==null) return true;
        boolean left = isValid(root.left,l,root.val);
        //root.val should always be greater than its left child value and less  that its right chil value
        if(root.val <=l || root.val>=r) return false;
        boolean right = isValid(root.right,root.val, r);
        return left &&  right;

    }
}