Amazon interview question

Leetcode style tree question no twist