Amazon Interview Question

Burning tree question from leetcode