Open utterances-bot opened 2 years ago
type InorderTraversal<T extends TreeNode | null,
L extends TreeNode | null = T extends TreeNode ? T['left'] : null,
R extends TreeNode | null = T extends TreeNode ? T['right']: null
> =
T extends null
? []
: [...InorderTraversal<L>, ...(T extends TreeNode ? [T['val']] : []), ...InorderTraversal<R>]
My solution:
type InorderTraversal<T extends TreeNode | null> = T extends TreeNode
? [...InorderTraversal<T['left']>, T['val'], ...InorderTraversal<T['right']>]
: []
InOrder Traversal
This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.
https://ghaiklor.github.io/type-challenges-solutions/en/medium-inordertraversal.html