-
Notifications
You must be signed in to change notification settings - Fork 0
776. Split BST #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
potrue
wants to merge
1
commit into
main
Choose a base branch
from
776
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
776. Split BST #47
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| 一応ここに書いたコードはすべてLLMに聞いて期待通りに動作するだろうとは言われたコードですが、LeetCodeなどで試せているわけではないのでもしかしたら動かないかもしれないです。動かないと思ったら教えてください。 | ||
|
|
||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<TreeNode*> splitBST(TreeNode* root, int target) { | ||
| if (root == nullptr) { | ||
| return {nullptr, nullptr}; | ||
| } | ||
| if (root->val <= target) { | ||
| vector<TreeNode*> right_split = splitBST(root->right, target); | ||
| TreeNode* lesser_root_of_right = right_split[0]; | ||
| TreeNode* greater_root_of_right = right_split[1]; | ||
| root->right = lesser_root_of_right; | ||
| return {root, greater_root_of_right}; | ||
| } | ||
| else { | ||
| vector<TreeNode*> left_split = splitBST(root->left, target); | ||
| TreeNode* lesser_root_of_left = left_split[0]; | ||
| TreeNode* greater_root_of_left = left_split[1]; | ||
| root->left = greater_root_of_left; | ||
| return {lesser_root_of_left, root}; | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/Ryotaro25/leetcode_first60/pull/50/files | ||
| https://github.com/Mike0121/LeetCode/pull/16/files | ||
| https://github.com/tokuhirat/LeetCode/pull/47/files | ||
|
|
||
| 最初見たときループ版が何をやっているのかあまりわからなかったがしばらく考えているとなんとなく分かった。 | ||
| ループ版も書いてみる。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<TreeNode*> splitBST(TreeNode* root, int target) { | ||
| TreeNode* lesser_root = nullptr; | ||
| TreeNode* greater_root = nullptr; | ||
| TreeNode** lesser_subtree_root_to_fill = &lesser_root; | ||
| TreeNode** greater_subtree_root_to_fill = &greater_root; | ||
| TreeNode* node = root; | ||
| while (node) { | ||
| if (node->val <= target) { | ||
| *lesser_subtree_root_to_fill = node; | ||
| TreeNode* tmp = node->right; | ||
| node->right = nullptr; | ||
| lesser_subtree_root_to_fill = &node->right; | ||
| node = tmp; | ||
| } | ||
| else { | ||
| *greater_subtree_root_to_fill = node; | ||
| TreeNode* tmp = node->left; | ||
| node->left = nullptr; | ||
| greater_subtree_root_to_fill = &node->left; | ||
| node = tmp; | ||
| } | ||
| } | ||
| return {lesser_root, greater_root}; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 変数名の組み合わせはsmaller, greaterとすると対称的過ぎてx < target, x > targetみたいな印象になる気がする(x <= target, x > targetではなく) | ||
| ただ別にlesserでもその印象はそんなに変わらないかも・・・ | ||
|
|
||
| 再帰でやると全部のノードについて関数を呼び出さなきゃいけないので時間計算量がO(N)になるが、ループだと必要なポインタだけ変えていくので階層分で済んで(バランスされた木なら)O(logN)になる。 | ||
| (再帰でも空間計算量はO(logN)かな?) | ||
|
|
||
| あと、番兵をnewで作るときは関数を終わるときにdeleteしないとメモリーリークになってしまうっぽい。 | ||
| ただ、`TreeNode lesser_bst_node()`みたいな感じで作ればスコープを抜けたときに自動で解放されるらしい。 | ||
|
|
||
| 関数の返り値は本当はstd::pairとかにしたい気がする。 | ||
|
|
||
| ## 最終コード | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<TreeNode*> splitBST(TreeNode* root, int target) { | ||
| TreeNode* lesser_root = nullptr; | ||
| TreeNode* greater_root = nullptr; | ||
| TreeNode** lesser_subtree_root_to_find = &lesser_root; | ||
| TreeNode** greater_subtree_root_to_find = &greater_root; | ||
| TreeNode* node = root; | ||
| while (node) { | ||
| if (node->val <= target) { | ||
| *lesser_subtree_root_to_find = node; | ||
| TreeNode* tmp = node->right; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tmp は意味がある変数なので、next_node_to_split ぐらいでも良いかなと思いました。 |
||
| node->right = nullptr; | ||
| lesser_subtree_root_to_find = &node->right; | ||
| node = tmp; | ||
| } | ||
| else { | ||
| *greater_subtree_root_to_find = node; | ||
| TreeNode* tmp = node->left; | ||
| node->left = nullptr; | ||
| greater_subtree_root_to_find = &node->left; | ||
| node = tmp; | ||
| } | ||
| } | ||
| return {lesser_root, greater_root}; | ||
| } | ||
| }; | ||
| ``` | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lesser_insert_position など node そのものではないことが変数名からわかると、L96 の操作の意味が読みやすいと思いました。