Dynamic Programming and trees