Amazon Interview Question

Leetcode style tree question no twist