Fixed a bug in build tree from sorted list