Given a Binary Tree with children Nodes, Return a random Node with equal Probability of selecting any Node in tree.
Consider the given tree with root as 1.
10 / \ 20 30 / \ / \ 40 50 60 70
Input : getRandom(root); Output : A Random Node From Tree : 3 Input : getRandom(root); Output : A Random Node From Tree : 2
A simple solution is to store Inorder traversal of tree in an array. Let the count of nodes be n. To get a random node, we generate a random number from 0 to n-1, use this number as index in array and return the value at index.
An alternate solution is to modify tree structure. We store count of children in every node. Consider the above tree. We use inorder traversal here also. We generate a number smaller than or equal count of nodes. We traverse tree and go to the node at that index. We use counts to quickly reach the desired node. With counts, we reach in O(h) time where h is height of tree.
10,6 / \ 20,2 30,2 / \ / \ 40,0 50,0 60,0 70,0 The first value is node and second value is count of children.
We start traversing the tree, on each node we either go to left subtree or right subtree considering whether the count of children is less than random count or not.
If the random count is less than the count of children then we go left else we go right.
Below is the implementation of above Algorithm. getElements will return count of children for root, InsertChildrenCount inserts children data to each node, RandomNode return the random node with the help of Utility Function RandomNodeUtil.
Time Complexity of randomNode is O(h) where h is height of tree. Note that we are either moving to right or to left at a time.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Select a Random Node from a Singly Linked List
- Select a random number from stream, with O(1) space
- Probability that a random pair chosen from an array (a[i], a[j]) has the maximum sum
- Probability of choosing a random pair with maximum sum in an array
- Random number generator in arbitrary probability distribution fashion
- Probability of getting a perfect square when a random number is chosen in a given range
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Minimum size binary string required such that probability of deleting two 1's at random is 1/X
- Generate integer from 1 to 7 with equal probability
- Probability of a random pair being the maximum weighted pair
- Find an index of maximum occurring element with equal probability
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Clone a Binary Tree with Random Pointers
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Random Tree Generator Using Prüfer Sequence with Examples
- Find farthest node from each node in Tree
- Count of all possible Paths in a Tree such that Node X does not appear before Node Y
- Farthest distance of a Node from each Node of a Tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Largest sub-tree having equal no of 1's and 0's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.