Skip to content

LeetCode mode: run plain functions and your own node structs - #37

Merged
sumnoon merged 1 commit into
mainfrom
feat/leetcode-general
Sep 28, 2026
Merged

sumnoon merged 1 commit into
mainfrom
feat/leetcode-general

Conversation

@sumnoon

@sumnoon sumnoon commented Sep 28, 2026

Copy link
Copy Markdown
Owner

Problem

LeetCode mode only accepted class Solution with the built-in ListNode/TreeNode. A common learner program such as

struct Node { int val; Node* left; Node* right; Node(int val) { ... } };
void pre_order(Node* root) { ... }

failed immediately with LeetCode mode needs a class Solution. Even past that check, Node* was not a type the harness could build.

What changes

Which function runs

  • With a class Solution: its first public method, as before.
  • Otherwise: the free function that no other function calls. Recursion into itself is fine, so height() is treated as a helper and isBalanced() runs.
  • If several qualify and the code has a main(), the first one that main() calls wins.
  • The editor footer says what will run (Calls pre_order(root) for you), and the test-case placeholder lists the parameter names.

Node structs

  • Any struct with one scalar value field and one pointer to itself (a list) or two (a binary tree) is built from the test case, whatever the field names (data/next, key/l/r, …). Multi-declarator fields like T *l, *r; work too.
  • Nodes are made with the struct's own constructor when it takes the value; otherwise the value field is set directly. Links are always reset to nullptr, because the harness owns the shape.
  • Trees use LeetCode's level order with null gaps, via vector<optional<T>>, so string-valued trees work. Printing uses the same generic code.
  • ListNode/TreeNode are now just this generator fed LeetCode's definitions, still added when used but not defined.
  • A struct that can't be built says why, e.g. builds G* when G has one value field and one G* link (a list) or two (a tree).

Pasting a whole program

  • If the code has its own main(), a #define main stackbloom_pasted_main sets it aside without moving any lines, and the test case drives the chosen function.
  • If the test case is empty, the program runs unchanged. The hint under the test case says so.

Checks

Compiled and traced eight generated programs through the real tracer:

Case Output
your pre_order as pasted (constructor does val = val) four garbage values, faithfully
same with this->val = val, no main 1 2 3 4
same with main, empty test case own main() runs, 1 2 3 4
struct Node { int data; Node* next; } without a constructor, reverse [3,2,1]
string tree T *l, *r, main picks mirror over count ["a",null,"b",null,"c"]
height + balanced true (entry = balanced)
Solution::maxDepth(TreeNode*) 3
Solution::middle(ListNode*) [3,4,5]

Trace stops only land in the pasted code and the generated main, never in the harness. web/tests/leetcode.test.mjs goes from 5 to 9 tests (17 Node tests in total, all passing). Also checked in the browser: footer, hint, badge and a run of pre_order from root = [1,2,4,3].

README and docs/usage.md are updated.

🤖 Generated with Claude Code

LeetCode mode only took a class Solution and the built-in ListNode and
TreeNode, so a pre_order(Node* root) with its own struct Node stopped at
"needs a class Solution". Now:

- Without a Solution class it calls the free function no other function
  calls; if several qualify, the first one a pasted main() calls.
- Any struct with one value field and one self-pointer (a list) or two (a
  tree) is built from the test case, whatever its field names, through its
  constructor or by setting the value field. ListNode and TreeNode are the
  same generator with LeetCode's definitions.
- A pasted main() is renamed with #define so line numbers stay put; with
  an empty test case the program runs as it is.

The editor footer names the function that will be called.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sumnoon
sumnoon merged commit fa707ae into main Sep 28, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant