TikTok interview question

a tricky binary tree problem that took me 90min to solve