refactor distinct_tree_values