Check every C next pointer before printing levels
The C harness for Org Chart Row Navigation printed each level's values before checking each node's next pointer, and on a mismatch it closed the brackets and returned without a message. A wrong next on the last node of the bottom row therefore printed exactly the expected levels, and the solution in #370 passed 5/5 in C while C++, Java, JavaScript and Python fail it 1/5.
json_next_tree now checks every next pointer before printing anything. On a mismatch it reports "next pointers do not match the tree level order.", the same message the other languages raise, in the result's error field as json_list does. A candidate case without an expected answer then shows as failed instead of showing the message as its output.
Review turned up two more problems. The queue was a fixed 4096-entry array on the stack, and a candidate case with 5000 nodes (the problem allows 6000) ended the run with "stack smashing detected", on main as well. It is now allocated with malloc and doubled through temporary pointers. If an allocation fails, both buffers are freed and the harness reports "Out of memory while checking next pointers." A returned node that is its own child also passed every next check while the queue doubled until memory ran out, so a node already in the queue is now refused with "Returned tree must not reach a node twice.", the message json_tree uses since #388. The queue grows only after a child passes that check, and an empty tree still prints [] before anything is allocated.
For the solution in #370:
| Case | Before | After |
|---|---|---|
| sparse final level | [[1],[2,3],[4,5,7]], passes | message, fails |
| empty tree | [], passes | [], passes |
| single node | [[1]], passes | message, fails |
| missing middle children | [[1],[2,3],[5,7]], passes | message, fails |
| one child | [[1],[2]], passes | message, fails |
Validation on Ubuntu 24.04 under WSL2, over 8ecf93b:
- The toolchain test in
tests/browser/compiler-explorer.test.jsruns the judge's five cases plus a 64-node tree that fills the first queue exactly, with every node holding the same value, and a 6000-node tree. A correct solution must print every expected value. A wrongnexton the bottom row's last node, or on the root's left child pointing back at the root, must be reported as a mismatch. A last node that is its own left child, a last node whose left child is the root, and a root whose two children are the same node must be reported as a repeat. Builds whosereallocalways fails or fails only for the level ends, and two whosemallocloses only the queue or only the level ends, must report running out of memory exactly where an allocation is lost, and the self-child and back-to-root solutions must still report the repeat up to 64 nodes. Onmainthe test fails at its first build with "stack smashing detected", and it also fails when the harness is weakened by hand, for example to check only the last pointer of a level, only whether a pointer is NULL, or values instead of addresses. - Under
-fsanitize=address,undefined, these solutions and forcedmallocandreallocfailures run without a report.
Closes #370
Summary by cubic
Fixes the C harness for "Populating Next Right Pointers in Each Node II" so it validates every next pointer before printing levels, instead of letting a wrong next on the last node of the bottom row pass silently.
- On a mismatch,
json_next_treenow prints"next pointers do not match the tree level order.", matching the C++, Java, JavaScript, and Python harnesses, and it also refuses any node already reached by printing"Returned tree must not reach a node twice." - The traversal queue now grows dynamically instead of holding a fixed 4096 entries, since a candidate case can reach the problem's 6000-node limit; a failed allocation reports
"Out of memory while checking next pointers." - Adds a test covering a correct solution, a ringed bottom row, a self-child, a 6000-node tree, and failing
reallocandmallocpaths that verify the out-of-memory message.
Closes #370.