validateBST.md
October 2, 2024 ยท View on GitHub
Problem statement:
Given the root of a binary tree, return true if it is a valid Binary Search Tree(BST), otherwise return false.
A valid BST follows the below constraints:
- The left subtree of every node contains only nodes with keys less than the node's key.
- The right subtree of every node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Examples:
Example1:
Input: root = [2,1,3] Output: true

Example2:
Input: root = [4,2,5,3,6] Output: false

Algorithmic Steps This problem is solved by Depth-First-Search(DFS) using recursion. The algorithmic approach can be summarized as follows:
-
Create a function(
isValidBST) to validate the binary search tree, which accepts root node as input. -
Create and invoke a DFS function(
dfs) to verify the boundaries of each node value. This function is called with root node, followed by negative infinity and positive infinity as boundaries. -
The DFS function can be created with arguments as specified in previous step.
- At first, add a base case check, returning
falseif the tree node is null. - If the node value is not between left and right boundaries, return
falseindicating that invalid BST. - Recursively invoke DFS function on left and right subtrees. This is because each subtree of BST should be a valid BST as well.
- At first, add a base case check, returning
-
Returning the DFS function call in step2 determines whether given tree is a BST or not.
Time and Space complexity:
This algorithm has a time complexity of O(n), where n is the number of nodes in either of the binary trees. This is because DFS visits each node exactly once to compare its boundaries.
It requires a space complexity of O(n) because call stack requires at most length of n.