Solution: Validate Binary Search Tree

Let's solve the Validate Binary Search Tree problem using the Tree Depth-First Search pattern.

Statement#

Given the root of a binary tree, check whether it is a valid binary search tree (BST).

A binary tree is a valid BST if for every node:

  • The value of the node is greater than or equal to the value of its left node.

  • The value of the node is less than or equal to the value of its right node.

  • Both the left and right subtrees are valid BSTs.

Constraints:

  • 104-10^{4} \leq Node.data 104\leq 10^{4}

  • The tree contains nodes in the range [1,500][1, 500].

Solution#

For this solution, we will recursively perform the inorder (left subtree, root, right subtree) traversal on the binary tree. We will be using inorder traversal to get elements in sorted order. We will also use a prev variable to hold the value of the previous node visited during the inorder traversal.

We initialize the prev variable with negative infinity. Then, we perform the inorder traversal on the tree and check whether the current node’s value is less than the prev value. If it is, return FALSE since it is not a valid BST. If not, update the value of prev with the current node’s value, and continue the traversal. If all nodes have been traversed and no node’s value was less than the prev value, it means that the tree is a valid BST. Therefore, we return TRUE.

Let’s look at an example below:

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev -INF Let's check whether the following tree is a valid BST.

1 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev -INF Start the inorder traversal of the binary tree.

2 of 19

Created with Fabric.js 3.6.6 prev -INF 10 3 13 2 4 11 12 Traverse the root to the left node of the root.

3 of 19

Created with Fabric.js 3.6.6 prev -INF 10 3 13 2 4 11 12 Recursively perform inorder traversal on the left child of the node if it's not the leaf node.

4 of 19

Created with Fabric.js 3.6.6 prev -INF 10 3 13 2 4 11 12 Check if the left child is the leaf node.

5 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 2 Since the left child is the leaf node, and it's value is greater than the prev value, update the prev value.

6 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 3 Recursively perform inorder traversal on the parent node, and check whether its value is greater than the prev value. If it is, update the prev value.

7 of 19

Created with Fabric.js 3.6.6 prev 3 10 3 13 2 4 11 12 Recursively perform inorder traversal on the right child of the node.

8 of 19

Created with Fabric.js 3.6.6 prev 3 Check if the right child is the leaf node. 10 3 13 2 4 11 12

9 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 4 Since the right child is the leaf node, and its value is greaterthan the prev value, update the prev value.

10 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 10 Recursively perform inorder traversal on the parent node, and check whether its value is greater than the prev value. If it is, update the prev value.

11 of 19

Created with Fabric.js 3.6.6 prev 10 10 3 13 2 4 11 12 Recursively perform inorder traversal on the right child of the node.

12 of 19

Created with Fabric.js 3.6.6 prev 10 10 3 13 2 4 11 12 If it is not the leaf node, recursively perform inorder traversal on the left child in the subtree.

13 of 19

Created with Fabric.js 3.6.6 prev 10 Check if the left child is the leaf node. 10 3 13 2 4 11 12

14 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 11 Since the left child is the leaf node, and its value is greater than the prev value, update the prev value.

15 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 13 Recursively perform inorder traversal on the parent node, and check whether its value is greater than the prev value. If it is, update the prev value.

16 of 19

Created with Fabric.js 3.6.6 prev 13 10 3 13 2 4 11 12 Recursively perform inorder traversal on the right child ofthe node.

17 of 19

Created with Fabric.js 3.6.6 prev 13 Check if the right child is the leaf node. 10 3 13 2 4 11 12

18 of 19

Created with Fabric.js 3.6.6 10 3 13 2 4 11 12 prev 13 Since the right child is the leaf node and its value is smaller than the prev value, we return FALSE because it is not a valid BST.

19 of 19

Let’s implement the algorithm as discussed above:

main.py
BinaryTree.py
TreeNode.py
Validate Binary Search Tree

Time complexity#

The time complexity of this solution is O(n)O(n), where nn represents the number of nodes in the binary tree.

Space complexity#

The space complexity of this solution is O(n)O(n). This is because our recursive algorithm uses space on the call stack.

Validate Binary Search Tree

Tree Breadth-first Search: Introduction