Open
Conversation
ryosuketc
reviewed
Aug 4, 2025
| if root is None: | ||
| return False | ||
|
|
||
| node_and_val = [(root, root.val, root.val)] |
There was a problem hiding this comment.
root.val は root からアクセスできるのでスタックに積む必要はないと思います。node_and_path_sum などにするとよさそうですね (最初、2, 3 番目の要素が何を示しているのか戸惑いました)。
| node_and_val = [(root, root.val, root.val)] | ||
|
|
||
| while node_and_val: | ||
| node, val, sum = node_and_val.pop() |
There was a problem hiding this comment.
組み込みの sum を上書きしているので、避けたほうがいいですね。path_sum とするか、やむを得なければ sum_ でしょうか。
https://peps.python.org/pep-0008/#descriptive-naming-styles
single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g. :
tkinter.Toplevel(master, class_='ClassName')
| return False | ||
|
|
||
| targetSum = targetSum - root.val | ||
| if targetSum == 0 and root.left is None and root.right is None: |
There was a problem hiding this comment.
細かいしどちらでもいいですが、
if root.left is None and root.right is None and targetSum == 0:
...if root.left is None and root.right is None:
if targetSum == 0:
...としたくなりますね。葉であるかどうかが前提条件なおで先にチェックしたいです。
tokuhirat
reviewed
Aug 4, 2025
| if node.left is None and node.right is None: | ||
| if sum == targetSum: | ||
| return True | ||
| if node.right: |
There was a problem hiding this comment.
None であるか確認している場合とそうでない場合が混在している点が気になりました。
また、L20 では left を先に書いていて、L23-26 では right を先に書いている点も少し気になりました。
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.
112. Path Sum
次回予告: 121. Best Time to Buy and Sell Stock