shlogg · Early preview
Prashant Mishra @prashantrmishra

Validating A Binary Search Tree In O(n) Time Complexity

Validating a Binary Search Tree (BST) in O(n) time complexity. A BST is valid if for every node, its value is greater than all values in its left subtree and less than all values in its right subtree.

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) ret...