WNS interview question

Medium level leetcode question on linked list and tree