Made traversal for tree cost and newick coonversion tail recursive