A binary tree of N nodes is "curious" if it is a binary tree whose node values ββare 1, 2, .., N and satisfy the property that
- Each internal node of the tree has exactly one descendant, which is larger than it.
- Each number in 1,2, ..., N appears in the tree exactly once.
Curious binary tree example
4 / \ 5 2 / \ 1 3
Can you give an algorithm for generating a uniformly random curious binary tree of n nodes that works in O (n) guaranteed time?
Suppose you have access to a random number generator that can give you a (uniformly distributed) random number in the range [1, k] for any 1 <= k <= n. Suppose the generator runs in O (1).
O (nlogn) time solution will also get my upvote.
Please follow the usual definitions of labeled binary trees that are excellent to view various curious binary trees.
algorithm puzzle
Aryabhatta
source share