Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3376 - InorderTraversal #20038

Open
fengjinlong opened this issue Nov 29, 2022 · 1 comment
Open

3376 - InorderTraversal #20038

fengjinlong opened this issue Nov 29, 2022 · 1 comment
Labels
3376 answer Share answers/solutions to a question en in English

Comments

@fengjinlong
Copy link

// your answers
const tree1 = {
  val: 1,
  left: null,
  right: {
    val: 2,
    left: {
      val: 3,
      left: null,
      right: null,
    },
    right: null,
  },
} as const
interface TreeNode {
    val: number;
    left: TreeNode | null;
    right: TreeNode | null
}
type A = InorderTraversal1<typeof tree1> // [1, 3, 2]
type InorderTraversal1<T extends TreeNode | null> = [T] extends [TreeNode] ? [
    ...InorderTraversal1<T['left']>,
    T['val'],
    ...InorderTraversal1<T['right']>
]:[]
@fengjinlong fengjinlong added answer Share answers/solutions to a question en in English labels Nov 29, 2022
@misterhomer1992
Copy link

Hi. Could you please explain, why did you use [T] extends [TreeNode] over just T extends TreeNode, whats the point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3376 answer Share answers/solutions to a question en in English
Projects
None yet
Development

No branches or pull requests

2 participants