Binary Tree. Identify element v in T such that k-1 elements in T are
smaller than v
I have this problem I need to solve. I am not looking for an answer, but a
hint towards where I should go. I have an algorithm, but it is not O(log
n).
Given Binary Tree T and a positive integer k that is no greater than the
number of nodes in T, write pseudocode that identifies an element v in T
such that precisely k − 1 elements in T are smaller than v. Your
code should take time at most proportional to the height of T.
The basic idea I have here is that I would first check the left tree,
checking the size of the left tree. If the size of the left tree is
greater than k-1, than I continue searching left. Else I would search
Right. If the entire left tree does not contain a node with k-1 elements
then I search the right subtree. The problem is that I know this isn't
O(log n) because worst case I would have to search every node in the tree.
Is there something I am missing? Any hints or help would be awesome, but
please don't just give me an answer.
No comments:
Post a Comment