Microsoft interview question

Leetcode medium. Some array and tree questions.