Nested Todo List
Problem statement
Process operations against an initially empty nested todo list. Items form an ordered forest.
["ADD", id, parentId, text]adds a pending item. AparentIdof-makes it a root; otherwise it becomes the newest child of that parent.["TOGGLE", id]switches that item betweenPENDINGandDONE. Toggling one item does not change descendants or ancestors.["GET", id]returnsid|parentId|status|text.["RENDER"]returns every item in preorder. Each line isdepth|id|status|text, roots and siblings retain insertion order, and lines are joined by a newline.
Return the values produced by GET and RENDER operations in order.
Function
runNestedTodoList(operations: String[][]) → String[]Examples
Example 1
operations = [["ADD","1","-","Plan"],["ADD","2","1","Draft"],["ADD","3","1","Review"],["TOGGLE","2"],["GET","2"],["RENDER"]]return = ["2|1|DONE|Draft","0|1|PENDING|Plan\n1|2|DONE|Draft\n1|3|PENDING|Review"]The two children retain insertion order. Toggling item 2 changes only that item.
Constraints
1 <= operations.length <= 20000.- IDs are unique non-empty strings containing neither
|nor whitespace. - Every non-root parent exists before its child is added.
- Every
GETandTOGGLEreferences an existing item. - Text is non-empty and contains neither
|nor a newline. - The total number of rendered item lines is at most
200000.