TikTok Interview Question

SQL: tree construct python: lc easy