Which will return: Peter and Amanda | Eklavya Online

Which will return: Peter and Amanda

If you want to find out the number of descendants for a node, all you need is the left_val and right_val of the node for which you want to find the  descendants  count.

The formula is

No. of descendants = (right_val – left_val -1) /2

So,  for 6 -11 Amanda, (11 – 6 – 1) /2 =  2 descendants

for 1-12  Peter, (12 – 1 -1 ) / 2 = 5 descendants.

for 3-4   Mary, (4 -3 – 1) / 2 =  0, means it is a child and has no descendants.

The modified preorder traversal is a little more complicated to understand, but is very useful.