Recursive solution computing both parts at the same time, sharing a single mutable iterator. A shared stack is used to store the scores for child nodes temporarily.