Build nested comments from flat list with parentId

Convert a flat list of comments into a nested (tree) structure for frontend rendering.

Input: an array comments, each with:

  • id
  • parentId (root comments may have null/0/missing; state you...

Example

Unlock to view complete problem details

and practice with sample input/output

Was this article helpful?

View Test Cases & Run Code requires membership

Standard Input
Execution Result: