Count all pair of leaf nodes which are at most K distance apart
for ( int l = 1 ; l < left . size ( ) ; l ++ ) { for ( int r = 0 ; r < right . size ( ) ; r ++ ) { if ( l + r <= distance ) { result += left [ l ] * right [ r ] ; } } }