> For the complete documentation index, see [llms.txt](https://docs-57.gitbook.io/data-structure-and-algorithms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs-57.gitbook.io/data-structure-and-algorithms/problems/tree/valid-binary-search-tree.md).

# Valid Binary Search Tree

Given the `root` of a binary tree, determine if it is a valid binary search tree (BST).

A valid BST is defined as follows:

* The left subtree of a node contains only nodes with keys less than the node's key.
* The right subtree of a node contains only nodes with keys greater than the node's key.
* Both the left and right subtrees must also be binary search trees.

![image](https://github.com/VikashChauhan51/algorithms/assets/14816038/191c314f-d3a1-4e97-8bc2-2a4019ec6bfa)

### Solution

```csharp
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public bool IsValidBST(TreeNode root)
    {

        return validate(root, null, null);
    }
    public bool validate(TreeNode root, int? low, int? high)
    {
        if (root == null)
        {
            return true;
        }

        if ((low != null && root.val <= low) || (high != null && root.val >= high))
        {
            return false;
        }

        return validate(root.right, root.val, high) && validate(root.left, low, root.val);
    }
}
```

Let's analyze the time and space complexity of the given algorithm:

* **Time Complexity**: The time complexity of this algorithm is **O(n)**, where n is the number of nodes in the binary tree. This is because in the worst case, the algorithm needs to visit all the nodes of the tree to check if it's a valid BST.
* **Space Complexity**: The space complexity of this algorithm is **O(h)**, where h is the height of the binary tree. This is because the maximum amount of space used by the call stack during the recursive calls is equal to the height of the tree. In the worst case, when the tree is skewed, the height of the tree is equal to the number of nodes, so the space complexity would be **O(n)**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs-57.gitbook.io/data-structure-and-algorithms/problems/tree/valid-binary-search-tree.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
