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