-
Notifications
You must be signed in to change notification settings - Fork 0
112.path sum #24
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
base: main
Are you sure you want to change the base?
112.path sum #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| 問題: https://leetcode.com/problems/path-sum/description/ | ||
|
|
||
| ## step1 | ||
|
|
||
| 問題は | ||
| 1.あるTreeNodeとtargetSumという整数が渡される。 | ||
| 2.TreeNodeのrootからleafへの経路のvalの和でtargetSumになるものがあればtrue、なければfalseを返せ。 | ||
| というもの。 | ||
|
|
||
| targetSumからroot->valを引いたものをroot->leftとroot->rightと共に引数にして再帰で関数を回す方法で解いた。 | ||
|
|
||
| 時間計算量:O(N) | ||
| 空間計算量:O(N) | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int targetSum) { | ||
| if (!root) { | ||
| return false; | ||
| } | ||
| targetSum -= root->val; | ||
| if (targetSum == 0 && !root->left && !root->right) { | ||
| return true; | ||
| } | ||
| bool left_value = hasPathSum(root->left, targetSum); | ||
| bool right_value = hasPathSum(root->right, targetSum); | ||
| if (left_value || right_value) { | ||
|
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. return left_value || right_value;と書いたほうがシンプルだと思いました。
Owner
Author
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. ありがとうございます。 |
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 書き方が場当たり的になってしまった(trueを返す時の条件など)。copilotに短縮してもらった。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int targetSum) { | ||
| if (!root) return false; | ||
|
|
||
| targetSum -= root->val; | ||
|
|
||
| if (!root->left && !root->right) | ||
| return targetSum == 0; | ||
|
|
||
| return hasPathSum(root->left, targetSum) || | ||
| hasPathSum(root->right, targetSum); | ||
| } | ||
| }; | ||
|
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. 経路が見つかったあとも探索するので、プログラムの動作に若干違和感ありますね。 見つかった時点でTrueを返したほうがわかりやすいかもしれません 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. 下の深さ優先のコードのイメージですね
Owner
Author
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. レビューありがとうございます。 if (!node->left && !node->right && sum == targetSum)とすればよかったですが、思いつきませんでした。 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.
return hasPathSum(root->left, targetSum) ||
hasPathSum(root->right, targetSum);この部分のことですかね?
こちらについては、変更の意図がよく分かりませんでした。 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. はい、あげていただいた箇所の話です。
ご指摘いただきありがとうございます。 bool left_value = hasPathSum(root->left, targetSum);
bool right_value = hasPathSum(root->right, targetSum);
if (left_value || right_value) {
return true;
} else {
return false;
}を清書したものだと思い込んでいました。 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.
私もわかりかねますが、
Owner
Author
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. 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.
これは勘違いではないでしょうか。「下の深さ優先のコードのイメージですね」というのは、step3を指していて、早期リターンの例示かと思います。 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. @h-masder さんの最初のコメントは、一番上のコードの以下の部分に対して正当なコメントだと思います。 bool left_value = hasPathSum(root->left, targetSum);
bool right_value = hasPathSum(root->right, targetSum);
if (left_value || right_value) {
return true;
} else {
return false;
}@nicah4o さん
Owner
Author
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. @rikugame |
||
| ``` | ||
|
|
||
| ## step2 | ||
|
|
||
| - https://github.com/attractal/leetcode/pull/37/changes | ||
| bfsなどの方法 | ||
|
|
||
| 自分でもstack bfsをやってみた。 | ||
|
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. stackでLIFOに探索をしているので、深さ優先探索(DFS)ですね。
Owner
Author
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. レビューありがとうございます。 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. 幅優先探索に変形してみるとよいと思います
Owner
Author
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. class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
if (!root)
return false;
queue<pair<TreeNode*, int>> nodes_and_sums;
nodes_and_sums.push({root, root->val});
while (!nodes_and_sums.empty()) {
auto [node, sum] = nodes_and_sums.front();
nodes_and_sums.pop();
if (!node->left && !node->right) {
if (sum == targetSum)
return true;
}
if (node->left) {
nodes_and_sums.push({node->left, sum + node->left->val});
}
if (node->right) {
nodes_and_sums.push({node->right, sum + node->right->val});
}
}
return false;
}
};queueにすることで同じレベルの左右nodeを見てその下に向かう形になりますね。 |
||
|
|
||
| 時間計算量:O(N) | ||
| 空間計算量:O(N) | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int targetSum) { | ||
| if (!root) | ||
| return false; | ||
| stack<pair<TreeNode*, int>> nodes_and_sums; | ||
| nodes_and_sums.push({root, 0}); | ||
| bool result = false; | ||
| while (!nodes_and_sums.empty()) { | ||
| auto [node, sum] = nodes_and_sums.top(); | ||
| nodes_and_sums.pop(); | ||
| sum += node->val; | ||
| if (!node->left && !node->right && !result) { | ||
| result = (sum == targetSum); | ||
| } | ||
| if (node->left) { | ||
| nodes_and_sums.push({node->left, sum}); | ||
|
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. nodes_and_sums.emplace(node->left, sum);と書くとシンプルになると思います。
Owner
Author
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. レビューありがとうございます。 |
||
| } | ||
| if (node->right) { | ||
| nodes_and_sums.push({node->right, sum}); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| resultにboolで代入せずともtrue条件にsum == targetSumをいれればよかった。 | ||
|
|
||
| ## step3 | ||
|
|
||
| stackを練習した。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| bool hasPathSum(TreeNode* root, int targetSum) { | ||
| if (!root) | ||
| return false; | ||
| stack<pair<TreeNode*, int>> nodes_and_sums; | ||
| nodes_and_sums.push({root, root->val}); | ||
| while (!nodes_and_sums.empty()) { | ||
| auto [node, sum] = nodes_and_sums.top(); | ||
| nodes_and_sums.pop(); | ||
| if (!node->left && !node->right) { | ||
| if (sum == targetSum) | ||
| return true; | ||
| } | ||
| if (node->left) { | ||
| nodes_and_sums.push({node->left, sum + node->left->val}); | ||
| } | ||
| if (node->right) { | ||
| nodes_and_sums.push({node->right, sum + node->right->val}); | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| }; | ||
| ``` | ||
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.
left_value という変数名からは、左側の子ノード以下に関する何らかの値というニュアンスを感じました。
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.
そうですね。valueはこの場合boolの値以外もあるので良くなかったです。
has_Path_leftなどがよかったかもしれません。