leetcode problem solving
https://leetcode.com/problems/validate-binary-search-tree/
Following two solutions are simple to solve this problem.
Solution A. BST should maintain the following features.
- for any node, all values of nodes in the left subtree should be smaller than the node.
- for any node, all values of nodes in the right subtree should be bigger than the node.
Solution B. if the tree accepted following features,
- the maximum value of the left subtree is smaller than the parent node.
- the minimum value of the right subtree is bigger than the parent node.
Solution A is the key idea of latter solution with child comparison.
Solution B is the key idea of former solution with root comparison.
Here’s my C++ implementations of the solutions.
Min / Max validation with root comparison O(n)
|
|
Min / Max validation with child comparison O(n)
|
|