← Questions

Block tree with add, delete subtree and render

0
NotionCodingLast asked Jul 2025

A document is an ordered forest of blocks, each with an id and some text. Implement:

  1. add(id, parent_id, text): add a block as the last child of its parent, or as the last top-level block when it has no parent.
  2. delete(id): remove the block and everything nested under it.
  3. render(): return the document in pre-order, one line per block showing its depth, id and text, with siblings kept in the order they were added.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding