112 path sum - #25
Open
MA-yo-TA wants to merge 3 commits into
Open
Conversation
miyataka
reviewed
Jul 18, 2026
Comment on lines
+16
to
+20
| - https://github.com/naoto-iwase/leetcode/pull/29#discussion_r2455081026 | ||
| - 「もし葉でtargetSumになるような経路を返却するとしたらどうでしょうか?設問としてはTrue/Falseなのですが、単純にTrue/Falseを得るよりはpathを実際に知るほうが意味があるのかなと思ったので...自分が面接だったら質問しそうです。」 | ||
| - 葉から根へ向かうのは親を辿っていけば一本道でたどり着くので、探索しながら各ノードの親を覚えておく `node_to_parent: dict[TreeNode, TreeNode` 辞書を作って、葉から順番に辿っていけば良さそう。 | ||
| - 全部のパスを返す必要があるなら、見つかった時点での return をやめて全ノードを探索すれば良い。 | ||
| - パスを一つだけ見つければ良いという条件で再帰で書くなら↓のようになるだろうか。空リストより None が良いかもしれないのと、パスを探すならパスの存在自体が T/F の代わりになるので bool を返す必要はなさそう。全部のパスを返す場合は、返すのがパスのリスト = ノードのリストのリストになってだいぶ煩雑な気がする。 |
Comment on lines
+64
to
+66
| return self.hasPathSum(root.left, children_target_sum) or self.hasPathSum( | ||
| root.right, children_target_sum | ||
| ) |
There was a problem hiding this comment.
pythonに詳しくないのですが,こういう改行のほうが嬉しいかなと思いました.
pythonだと意味が変わっちゃうかもしれないのですが.
Suggested change
| return self.hasPathSum(root.left, children_target_sum) or self.hasPathSum( | |
| root.right, children_target_sum | |
| ) | |
| return self.hasPathSum(root.left, children_target_sum) or | |
| self.hasPathSum(root.right, children_target_sum) |
Owner
Author
There was a problem hiding this comment.
ありがとうございます。私も同じようなことは思ったのですが、ruff という標準的なフォーマッター(兼リンター)の一つをデフォルト設定で使ってこのようになるので今回はこれを採用しました。
見た目でいうと以下のようなものがよさそうでしょうか。(ただしこれも ruff でフォーマットすると元のコードに戻されます)
return (
self.hasPathSum(root.left, children_target_sum)
or self.hasPathSum(root.right, children_target_sum)
)細かいところを補足しますと、
- 括弧がないと改行で文が切れてしまってエラーになるのでカッコで囲む必要がある
- Python では演算子の後ではなく前に改行を入れるのが推奨されているイメージ
という感じのようです。
There was a problem hiding this comment.
再帰とiterative, 両方書いていてよいなと思いました.
全体的に読みやすかったです
| 他の方のコードでよく見かける `forntiers` という変数名を使ってみた。 | ||
|
|
||
| 再帰でも書ける(step1-2.py)。一本道だと再帰呼び出しの回数がノード数(最大 5000)になるので、注意が必要(Python の再帰呼び出し回数の上限はデフォルトだと 1000) | ||
|
|
There was a problem hiding this comment.
余裕があれば,実際にかかる時間やメモリを見積もってみてもいいかもしれません
Owner
Author
There was a problem hiding this comment.
ありがとうございます。余裕のある時は見積もるようにします
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/
次の問題:https://leetcode.com/problems/binary-tree-level-order-traversal/