According to your code, if all the distance are equal(in build function), the tree will become linear and time complexity will increase.