Code to build tree from the bottom