Wish Interview Question

leetcode 844 and a medium tree problem