donnemartin / donnemartin/interactive-coding-challenges
Bug in bst_validate solution
- Dominant language
- Python
- Stars
- 31.8k
- Forks
- 4.7k
- PR merge metrics
- No merged PRs in 30d
Description
The solution for [ bst_validate](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/bst_validate/bst_validate_solution.ipynb) won't work if the tree has a node with the value of -sys.maxsize.
A suggested tweak would be to use None instead of sys.maxsize and -sys.maxsize.
```python
class BstValidate(Bst):
def validate(self):
if self.root is None:
raise TypeError('No root node')
return self._validate(self.root)
def _validate(self, node, minimum=None, maximum=None):
if node is None:
return True
if not ((minimum is None or node.data>minimum) and (maximum is None or node.data<=maximum)):
return False
if not self._validate(node.left, minimum, node.data):
return False
if not self._validate(node.right, node.data, maximum):
return False
return True
```
I would be happy to prepare a PR if this makes sense to you.
Contributor guide
Assessment
This issue has not been assessed yet.