TikTok interview question

SQL: tree construct python: lc easy