Skip to content

776. Split BST#47

Open
dxxsxsxkx wants to merge 1 commit into779_kth_symbol_in_grammarfrom
776_split_bst
Open

776. Split BST#47
dxxsxsxkx wants to merge 1 commit into779_kth_symbol_in_grammarfrom
776_split_bst

Conversation

@dxxsxsxkx
Copy link
Copy Markdown
Owner

問題

https://www.lintcode.com/problem/847/description

LintCode の問題なので、LeetCode の同名の問題と若干要件が違います。

次の問題

3. Longest Substring Without Repeating Characters

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;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.firstsplit_trees.secondnullptr の場合は無いんでしたっけ(おそらく rootnullptr の場合のみ?)。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left_count == right_count かつ split_trees.firstsplit_trees.secondnullptr の場合は、split_trees.second = nullptr が返るので問題ないですね。

Comment on lines +25 to +28
auto split = splitHelper(root, v);

int left_count = countNode(split.first);
int right_count = countNode(split.second);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

return right_root;
}

return (left_root->val > right_root->val) ? left_root : right_root;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

制約がわかりませんでしたが、root = nullptr のときここでクラッシュあるいは未定義動作しそうですね。

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.

2 participants