Open
Conversation
PafsCocotte
reviewed
Feb 16, 2026
| // ---------------------------- | ||
| // Skewed Tree のテスト | ||
| // 再帰はスタックオーバーフローしやすいので、小さいサイズのみ | ||
| // 大きいサイズは BFS のみで測定 |
There was a problem hiding this comment.
通りすがりに失礼します。
DFSはstackで書けて、これはBFS同様にスタックオーバーフローを気にせずできます。
知っていらしたら問題ないです。
mamo3gr
reviewed
Feb 18, 2026
Comment on lines
+26
to
+29
| // 感覚的には木の構造によって、幅優先探索と、深さ優先探索を使い分けるのが良さそうだが、 | ||
| // 感覚の域を出ない。そもそもあっているかもわからない。 | ||
| // 浅いルートが多いやつだったり一本だけ驚異的に長いルートがあるなどの、長さに差がある木は、幅優先探索で、 | ||
| // あまり長さに差がない木は、深さ優先探索を選んだ方が良さそう。 |
There was a problem hiding this comment.
条件を満たす葉ノードが存在する浅さにも左右される気がしました。結局探索してみるまで分からないように思います。
mamo3gr
reviewed
Feb 18, 2026
|
|
||
| total += node.val; | ||
|
|
||
| if (isLeaf(node) && total == targetSum) { |
There was a problem hiding this comment.
node == null の場合を直前で弾いているので、isLeaf は non-null の node のみを受け付けるようにして良いと思いました。そうすると関数ではなくbooleanでも良さそうです。
また関数 isLeaf について、nodeがnullのときの挙動を仕様として与えてしまうと、後で変更するのが難しくなってしまうのも気になりました(変更したときに、呼び出し元の挙動を気にする必要が出てくる)。
mamo3gr
reviewed
Feb 18, 2026
Comment on lines
+103
to
+116
| var (node, total) = nodeWithTotal.removeFirst(); | ||
|
|
||
| if (node == null) { | ||
| continue; | ||
| } | ||
|
|
||
| total += node.val; | ||
|
|
||
| if (isLeaf(node) && total == targetSum) { | ||
| return true; | ||
| } | ||
|
|
||
| nodeWithTotal.add((node.left, total)); | ||
| nodeWithTotal.add((node.right, total)); |
There was a problem hiding this comment.
(個人的な好みです)
空行で処理のブロックを表していると思うのですが、もう少し荒い粒度でも差し支えないと感じました。
Suggested change
| var (node, total) = nodeWithTotal.removeFirst(); | |
| if (node == null) { | |
| continue; | |
| } | |
| total += node.val; | |
| if (isLeaf(node) && total == targetSum) { | |
| return true; | |
| } | |
| nodeWithTotal.add((node.left, total)); | |
| nodeWithTotal.add((node.right, total)); | |
| // キューから取り出す。nullなら飛ばす | |
| var (node, total) = nodeWithTotal.removeFirst(); | |
| if (node == null) { | |
| continue; | |
| } | |
| // 現在のノードが条件を満たすかチェック | |
| total += node.val; | |
| if (isLeaf(node) && total == targetSum) { | |
| return true; | |
| } | |
| // 子ノードに引き継ぐ | |
| nodeWithTotal.add((node.left, total)); | |
| nodeWithTotal.add((node.right, total)); |
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://leetcode.com/problems/path-sum/description/
次に解く問題:https://leetcode.com/problems/binary-tree-level-order-traversal/description/