Skip to content

Bug Report for invert-a-binary-tree #6040

Description

@nacho9900

Bug Report for https://neetcode.io/problems/invert-a-binary-tree

Problem

On the Invert Binary Tree problem (TypeScript), the starter code includes a commented-out TreeNode class, while the method signature references it:

invertTree(root: TreeNode | null): TreeNode | null

Leaving the class commented out works — the code compiles, runs, and submits without issues, so TreeNode is clearly being injected somewhere above the visible editor region.

The problem is purely in the editor: since the class doesn't exist in the visible context, TreeNode is underlined in red with Cannot find name 'TreeNode' the whole time you're writing your solution. It's confusing and uncomfortable to work with a permanent error on a solution that's actually correct, and it's easy for a user to assume they've broken something.

Uncommenting the class to silence the editor isn't a workaround either — it fails with Duplicate identifier 'TreeNode', colliding with the hidden definition.

Steps to reproduce

  1. Open Invert Binary Tree and select TypeScript.
  2. Write a solution with the TreeNode class left commented out → TreeNode is underlined in the editor, but the code runs and submits fine.
  3. Uncomment the class to fix the underline → Duplicate identifier 'TreeNode'.

Expected

TreeNode resolves in the editor's type-checker, so no error is shown for correct code.

Actual

The type-checker doesn't know about TreeNode, so the editor shows an error on every reference to it even though the submission passes.

Suggested fix

Expose TreeNode to the editor's type-checker without redeclaring it at runtime — e.g. an ambient declaration (declare class TreeNode { ... } in a .d.ts loaded into the editor instance) instead of a commented-out class.

Environment

TypeScript · Invert Binary Tree · browser editor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions