# Validate BST

Implement a function to check if a binary tree is a binary search tree.

Assume there are no duplicate elements

## Link here to the repo to solve the problem

π## π Tips

To be a binary search tree, it's not sufficient that the left. value <= current. value < right. value for each node. Every node on the left must be less than the current node, which must be less than all the nodes on the right.

If every node on the left must be less than or equal to the current node, then this is really the same thing as saying that the biggest node on the left must be less than or equal to the current node.

Rather than validating the current node's value against leftTree.max and rightTree.min, can we flip around the logic? Validate the left tree's nodes to ensure that they are smaller than current. value.

## π Solution 1

For this solution I just perform a check on every node, in which one of its children is not null. If is not null, I check that the numbers match the same as in a balanced binary tree. Then recursively perform the same checks on its children.

```
public boolean checkBST(BinaryTreeNode head) {
if (head == null) return true;
boolean checkLeft = true;
boolean checkRight = true;
if (head.left != null) checkLeft = (head.data > head.left.data) && checkBST(head.left);
if (head.right != null) checkRight = (head.data < head.right.data) && checkBST(head.right);
return checkLeft && checkRight;
}
```