Open
Conversation
mamo3gr
reviewed
Mar 19, 2026
Comment on lines
+28
to
+42
| if (left_count > right_count) { | ||
| return split_trees.first; | ||
| } else if (left_count < right_count) { | ||
| return split_trees.second; | ||
| } else { | ||
| if (!split_trees.first) { | ||
| return split_trees.second; | ||
| } | ||
| if (!split_trees.second) { | ||
| return split_trees.first; | ||
| } | ||
| return (split_trees.first->val > split_trees.second->val) ? | ||
| split_trees.first : | ||
| split_trees.second; | ||
| } |
There was a problem hiding this comment.
left_count == right_count の場合での処理の行数が多いので、先に読ませると消化しやすいように思いました。
Suggested change
| if (left_count > right_count) { | |
| return split_trees.first; | |
| } else if (left_count < right_count) { | |
| return split_trees.second; | |
| } else { | |
| if (!split_trees.first) { | |
| return split_trees.second; | |
| } | |
| if (!split_trees.second) { | |
| return split_trees.first; | |
| } | |
| return (split_trees.first->val > split_trees.second->val) ? | |
| split_trees.first : | |
| split_trees.second; | |
| } | |
| if (left_count == right_count) { | |
| if (!split_trees.first) { | |
| return split_trees.second; | |
| } | |
| if (!split_trees.second) { | |
| return split_trees.first; | |
| } | |
| return (split_trees.first->val > split_trees.second->val) ? | |
| split_trees.first : | |
| split_trees.second; | |
| } | |
| if (left_count > right_count) { | |
| return split_trees.first; | |
| } else { | |
| return split_trees.second; | |
| } |
出題サイトをざっと見た感じ制約を見つけられなかったのですが、left_count == right_count かつ split_trees.first も split_trees.second も nullptr の場合は無いんでしたっけ(おそらく root が nullptr の場合のみ?)。
There was a problem hiding this comment.
left_count == right_count かつ split_trees.first も split_trees.second も nullptr の場合は、split_trees.second = nullptr が返るので問題ないですね。
mamo3gr
reviewed
Mar 19, 2026
Comment on lines
+25
to
+28
| auto split = splitHelper(root, v); | ||
|
|
||
| int left_count = countNode(split.first); | ||
| int right_count = countNode(split.second); |
There was a problem hiding this comment.
first -> left, second -> right の変換を脳内で都度やる必要があり、先に置き換えてもらえると読み手としては助かります。
Suggested change
| auto split = splitHelper(root, v); | |
| int left_count = countNode(split.first); | |
| int right_count = countNode(split.second); | |
| auto split = splitHelper(root, v); | |
| TreeNode* left = split.first; | |
| TreeNode* right = split.second; | |
| int left_count = countNode(left); | |
| int right_count = countNode(right); |
mamo3gr
reviewed
Mar 19, 2026
| return right_root; | ||
| } | ||
|
|
||
| return (left_root->val > right_root->val) ? left_root : right_root; |
There was a problem hiding this comment.
制約がわかりませんでしたが、root = nullptr のときここでクラッシュあるいは未定義動作しそうですね。
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.
問題
https://www.lintcode.com/problem/847/description
LintCode の問題なので、LeetCode の同名の問題と若干要件が違います。
次の問題
3. Longest Substring Without Repeating Characters