From here:
def checkBST(root): prev_val = -1 for node in in_order_sort(root): if node.data <= prev_val: return False prev_val = node.data return True def in_order_sort(node): if node.left: yield from in_order_sort(node.left) yield node if node.right: yield from in_order_sort(node.right)
Looking for any suggestions on improving this. It's pretty concise.
The input data is constrained between \$0\$ and \$10^4\$ so I can "get away" with setting the initial value to -1. The input func name checkBST
is also predefined.
It seems that short of knowing you can validate a binary tree via an in-order traversal this would get complicated, but knowing that makes it straightforward?