I found a solution. Record another L
variable in each node to store the sum of end - start
of all ancestors. This value indicates the length of the substring ended at a particular node i.e. for leaf it is the length of the suffix. L
is updated whenever a tree node is added or a tree node is split.
Then the leaf-label is just n - leaf.L