Conversation
nodchip
reviewed
Feb 14, 2026
| def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: | ||
| if root is None: | ||
| return False | ||
| accumulate = root.val |
There was a problem hiding this comment.
動詞の原型を変数名に付けると、関数名のように感じられます。 accumulated はいかがでしょうか?
mamo3gr
reviewed
Feb 17, 2026
| frontier.append((root, total_val)) | ||
|
|
||
| while frontier: | ||
| frontier_node, val = frontier.popleft() |
There was a problem hiding this comment.
frontier から pop したので、これ以降は frontier_ が無くても大丈夫そうです。
Suggested change
| frontier_node, val = frontier.popleft() | |
| node, val = frontier.popleft() |
mamo3gr
reviewed
Feb 17, 2026
| if is_leaf and val == targetSum: | ||
| return True | ||
|
|
||
| if frontier_node.left: |
There was a problem hiding this comment.
冒頭で root is None 判定しているなら、ここでも is not None と判定するほうが、一貫性があって良いと思います。
反対に冒頭を not root でも良いです。
mamo3gr
reviewed
Feb 17, 2026
Comment on lines
+22
to
+24
| return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum( | ||
| root.right, targetSum - root.val | ||
| ) |
There was a problem hiding this comment.
ここ、左右対称な話なのに、フォーマッタに整形させると非対称みたいに見えちゃうんですよね…。
Suggested change
| return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum( | |
| root.right, targetSum - root.val | |
| ) | |
| left_has_path_sum = self.hasPathSum(root.left, targetSum - root.val) | |
| right_has_path_sum = self.hasPathSum(root.right, targetSum - root.val) | |
| return left_has_path_sum or right_has_path_sum |
のような選択肢も幅として持っておくといいのかもな、と思いました。
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/