Given a binary tree with TreeNode
like:
class TreeNode {
int data;
TreeNode left;
TreeNode right;
int size;
}
Where size
is the number of nodes in the (left subtree + right subtree + 1).
Print a random element from the tree in O(logn) running time.
Note: This post is different from this one, as it is clearly mentioned that we have a size
associated with each node in this problem.
PS: Wrote this post inspired from this.