112.path sum#24
Conversation
| - https://github.com/attractal/leetcode/pull/37/changes | ||
| bfsなどの方法 | ||
|
|
||
| 自分でもstack bfsをやってみた。 |
There was a problem hiding this comment.
stackでLIFOに探索をしているので、深さ優先探索(DFS)ですね。
ノードに子があったら、子から優先的に探索しているので。
There was a problem hiding this comment.
レビューありがとうございます。
気づきませんでしたが確かにそうでした。ありがとうございます。
There was a problem hiding this comment.
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を見てその下に向かう形になりますね。
自分が最初bfsといっていたものはある最深部まで行ってからその一つ上のレベルに戻ります。
| return hasPathSum(root->left, targetSum) || | ||
| hasPathSum(root->right, targetSum); | ||
| } | ||
| }; |
There was a problem hiding this comment.
経路が見つかったあとも探索するので、プログラムの動作に若干違和感ありますね。
見つかった時点でTrueを返したほうがわかりやすいかもしれません
There was a problem hiding this comment.
レビューありがとうございます。
リターンの条件を
if (!node->left && !node->right && sum == targetSum)とすればよかったですが、思いつきませんでした。
There was a problem hiding this comment.
経路が見つかったあとも探索するので、プログラムの動作に若干違和感ありますね。
return hasPathSum(root->left, targetSum) ||
hasPathSum(root->right, targetSum);この部分のことですかね?||は、短絡評価されるので、条件を満たすpathが見つかった時点で打ち切られると思います。
if (!node->left && !node->right && sum == targetSum)
こちらについては、変更の意図がよく分かりませんでした。
There was a problem hiding this comment.
はい、あげていただいた箇所の話です。
条件を満たすpathが見つかった時点で打ち切られると思います。
ご指摘いただきありがとうございます。
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.
こちらについては、変更の意図がよく分かりませんでした。
私もわかりかねますが、
とはいえ、まずはこちらが何の話をしているのかをちゃんと提示すべきでしたね。すみません。
There was a problem hiding this comment.
最初のh-masderさんのコメントでコメント箇所より下のDFSについての指摘である
これは勘違いではないでしょうか。「下の深さ優先のコードのイメージですね」というのは、step3を指していて、早期リターンの例示かと思います。
There was a problem hiding this comment.
@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 さん
一方で、Copilotの出したコードが短絡評価を活用したものになっていることについては、大丈夫そうでしょうか?
There was a problem hiding this comment.
@rikugame
ありがとうございます。文脈を読めていませんでした。
短絡評価というのは左辺の評価だけで判断できるというものですね。
そちらの変更に目を向けていなかったのでありがたい議論でした。
| } | ||
| 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.
return left_value || right_value;と書いたほうがシンプルだと思いました。
There was a problem hiding this comment.
ありがとうございます。
式の値を返すという考え方がまだ身についていないので積極的に取り入れます。
| if (targetSum == 0 && !root->left && !root->right) { | ||
| return true; | ||
| } | ||
| bool left_value = hasPathSum(root->left, targetSum); |
There was a problem hiding this comment.
left_value という変数名からは、左側の子ノード以下に関する何らかの値というニュアンスを感じました。
There was a problem hiding this comment.
そうですね。valueはこの場合boolの値以外もあるので良くなかったです。
has_Path_leftなどがよかったかもしれません。
| result = (sum == targetSum); | ||
| } | ||
| if (node->left) { | ||
| nodes_and_sums.push({node->left, sum}); |
There was a problem hiding this comment.
nodes_and_sums.emplace(node->left, sum);と書くとシンプルになると思います。
There was a problem hiding this comment.
レビューありがとうございます。
pushとの違いが分かりました。emplaceは直接構築できるのですね。
This problem: https://leetcode.com/problems/path-sum/
Next problem: https://leetcode.com/problems/binary-tree-level-order-traversal/description/