Date: 09-10-24
Problem
- In this problem we are given a potentially invalid BST and our function should return a boolean representing whether the BST is valid. For the BST to be valid, all of its node must be valid BST nodes. It should satisfy the BST property, which is when a nodes value is strictly greater than the values of every node to its left, and is less than or equal to the nodes to the right of it.
Example:
Approach 1:
- In this solution we can create a boolean case for each node as we traverse it. For instance, beginning at the root node the minimum possible value is negative inf and the maximum possible value is positive inf. Therefore, if the node satisfies this then we know this is a valid BST node. Then we move down to the left subtree and bring down that boolean case. However, in this case if we had a node of 5 moving down our left subtree, based on the current node we set it as the maximum (10) and now the new boolean case would be the current node value should be greater than negative inf and less than 10. If we move down to the right subtree we update the min value to the current node and now in this example the boolean case would be the current node should be greater than or equal to 5 and less than 10.
- In code this is accomplished using recursion.
Implementation
Complexity Analysis
- Time Complexity:
- Space Complexity: - d represents the depth of the tree, d is the worst case scenario