We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Valid BST
Valid BST
Sort by
recency
|
11 Discussions
|
Please Login in order to post a comment
scala
I think its more important to stick with the basics of it. Because basics are more vital. If we can understand its basics this problem can be solved in matter of seconds.
Could someone pls point us to a javascript or python solution?....
F# solution with comment:
Good approach is to just simulate preorder traversal. Since we know how the sequence is built we try to consume the elements in the order they are given to use. If the next element does not fit into our contraints of lo (lower bound) and hi (upper bound) then we propagate to the parent.
As the result, running preorder on a valid sequence will consume all elements, otherwise if the sequence could not be fully consumed a non-empty sequence of the remaining elements will be returned.
This could be easily mapped to YES/NO