# BST Validate

**Given the root node of a binary tree, write a function isBST that returns a boolean if the binary tree is also a valid binary search tree.**

- You can assume the tree won't be empty
- The tree will not contain duplicate values

```
// Example 1:
9
/ \
5 22
/ \ \
1 7 30
/
25
isBST(root); // true
// Example 2:
9
/ \
5 22
/ \ \
1 10 30
isBST(root); // false
// 10 > 9 but falls under its left path
```

The nodes in the tree will be standard binary nodes:

```
class BinaryNode {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
```

## Breakdown

Table of Contents

### Validate My Answer

There are different iterative and recursive solutions that all have the same time and space complexity. Feel free to approach it in whichever way you feel most comfortable, and I'll provide both an iterative and recursive approach in my solution.

For a binary search tree, ALL the descendants to the left of a node are smaller, and ALL the descendants to the right of a node are larger. You must ensure that a node is not only correct for its immediate parent, but also all of the ancestors.

The worst-case Big O time and space solution in both the iterative and recursive case are

*O(n)*where`n`

is the number of nodes in the tree.

Table of Contents

Test Results

Run your code and see results here...

### BST Validate

**Given the root node of a binary tree, write a function isBST that returns a boolean if the binary tree is also a valid binary search tree.**

- You can assume the tree won't be empty
- The tree will not contain duplicate values

```
// Example 1:
9
/ \
5 22
/ \ \
1 7 30
/
25
isBST(root); // true
// Example 2:
9
/ \
5 22
/ \ \
1 10 30
isBST(root); // false
// 10 > 9 but falls under its left path
```

The nodes in the tree will be standard binary nodes:

```
class BinaryNode {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
```