Google interview question

tree related problem, leetcode Easy to medium