Takefumi Yamamura's blog

This blog is for my memorandum.

Takefumi Yamamura's b!og

This blog is for my memorandum

Validate BST

problem

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

how to solve

Binary search tree means that all left nodes must be less than or equal to the current node and all right nodes must be more than or equal to the current node. So we have to check this condition to preserve the min and max value. When we branch left , the max gets updated. When we branch right, the min gets updated. If anything fails these checks, we stop and return false.

code

Remove all ads