← All Exercises

Recursive Type Definitions

advancedtypescript~30min

Background

Recursive types model tree and linked list structures, essential for parsing ASTs and hierarchical data.

Problem

Create a recursive Tree type with a value and array of children, then write a function that collects all values in DFS order.

Input

A Tree node with a value and children array.

Output

An array of all values in the tree in DFS order.

Sample tests

Sample 1

Input: {value:1, children:[{value:2,children:[]},{value:3,children:[]}]}}
Expected: [1, 2, 3]

Your solution

Learning objective

Understand recursive type definitions and recursive traversal functions with generics.