Skip to content

Add TypeScript goody for traversing a binary tree in level order #161

@miorel

Description

@miorel

Similar to #157, but level order! Since for level order, the level of the node is often important I think a good API might be roughly:

function traverseLevelOrder<T>(root: T | null | undefined): Generator<T[], void, void>;

In other words, we can yield an array of the nodes on each level.

Why a generic function?

While investigating how linked lists work on LeetCode, I noticed that the objects we get aren't necessarily instances of the listed classes like ListNode or TreeNode. So rather than writing a function that works on only TreeNode objects, we should write a function that works on anything that has a left and a right property. #157 offers some examples of how to do this.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesttypescriptAdventure Pack TypeScript issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions