Pregunta de entrevista de SingleStore

Tree based LC medium/hard question