Loading...
Loading...
Loading...
A binary tree is given, and it may not be a binary search tree. Find the largest subtree (a node and everything below it) that is a valid binary search tree with distinct keys, and print its number of nodes.
Example 1
Input
10 5 15 1 8 # 7
Output
3
Example 2
Input
2 1 3
Output
3
Try the problem first. Open one hint at a time.
Solve the children first and report four facts to the parent: is it a BST, its size, its smallest key, its largest key.
A node forms a BST when both children do, the left maximum is smaller than the node and the right minimum is larger.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor