Remove adjacent duplicates and handle tree input | Grammarly Interview Question