Open
Conversation
mamo3gr
reviewed
Mar 19, 2026
112/sol1.py
Outdated
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| diff = targetSum - root.val |
There was a problem hiding this comment.
まあ差分 (difference) であることはそうなんですが、new_target_sum みたいな命名が分かりやすいように感じました。
Owner
Author
There was a problem hiding this comment.
remainingを採用しようと思います。diffは分かりづらいですね。
mamo3gr
reviewed
Mar 19, 2026
112/sol2.py
Outdated
| return node.left is None and node.right is None | ||
|
|
||
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| que = deque() |
There was a problem hiding this comment.
中途半端な省略はしないほうが無難かと思います。この練習会では frontier という命名もよく見かけます。
Suggested change
| que = deque() | |
| queue = deque() |
Owner
Author
There was a problem hiding this comment.
frontierに慣れたいのもあるのでこちらを採用しようと思います。
mamo3gr
reviewed
Mar 19, 2026
112/sol2.py
Outdated
Comment on lines
+21
to
+28
| if node is None: | ||
| continue | ||
| current_sum += node.val | ||
| if self.is_leaf(node) and current_sum == targetSum: | ||
| return True | ||
| for child in [node.left, node.right]: | ||
| if child is not None: | ||
| que.append((child, current_sum)) |
There was a problem hiding this comment.
nodeのNoneチェックをするなら、childも気にせずappendしてしまえば良いと思いました。反対に、append時にNoneチェックするなら、関数の冒頭で root is None チェックをすれば良いと思います。
mamo3gr
reviewed
Mar 19, 2026
112/sol2.py
Outdated
| que = deque() | ||
| que.append((root, 0)) | ||
| while que: | ||
| node, current_sum = que.pop() |
There was a problem hiding this comment.
末尾からpopするなら、dequeではなくてstack (list) でも良さそうですね。
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/