Google Interview Question

tree related problem, leetcode Easy to medium