feat(cpp): add the default TreeNode constructor - #419
Merged
Conversation
LeetCode's own C++ boilerplate declares four TreeNode constructors, including the no-argument one. This header declares only two, so a solution that calls new TreeNode() compiles on the judge and fails to compile locally - the case local testing exists to catch. ListNode already carries the matching set, so the asymmetry is easy to miss until a tree problem hits it.
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LeetCode's own C++ boilerplate for tree problems declares the no-argument constructor:
testutils/cpp/LC_IO.hdeclares only the latter two. So a solution that callsnew TreeNode()— common in tree-building and serialize/deserialize problems — compiles on the judge and fails to compile locally:That is the exact case local testing exists to catch, and here it produces a false negative against code the judge accepts.
ListNodein this same header already carries the matching set of three, which makes the asymmetry easy to miss until a tree problem runs into it.Change
One line, restoring parity with both LeetCode's boilerplate and
ListNode.Verified with the CI command (
g++ -std=c++17 -O2 -o tests tests.cpp && ./tests): all 15 existing tests pass.Filed separately from #418 so the two can be judged on their own merits — that one is a bug fix, this one touches a public struct's API.