112. Path Sum#19
Open
hiro111208 wants to merge 1 commit into
Open
Conversation
liquo-rice
reviewed
Apr 27, 2026
| nodes_and_sums = [(root, root.val)] | ||
| while nodes_and_sums: | ||
| node, current_sum = nodes_and_sums.pop() | ||
| if node: |
There was a problem hiding this comment.
必要なさそうですかね?
必要だとしても、ネストを一段減らすために、if not node: continueの形にした方が良さそうです。
また、Noneかどうかのチェックの方法に一貫性がないのが気になります。
Comment on lines
+21
to
+22
| if node.left is None and node.right is None and current_sum == targetSum: | ||
| return True |
There was a problem hiding this comment.
current_sum == targetSumのチェックはifの中に入れて、一致しないならcontinueする方が良いと思います。
| @@ -1,6 +1,6 @@ | |||
| # LeetCode | |||
|
|
|||
| ## 問題: [Link Text](URL) | |||
There was a problem hiding this comment.
PRをマージしないにしても、テンプレフォルダーをコピーして、問題ごとにフォルダーを作ると、diffが綺麗に出て、(マージするなら)ローカルにファイルも残るので良いと思います。
| stack.append((node.left, current_sum + node.left.val)) | ||
| if node.right is not None: | ||
| stack.append((node.right, current_sum + node.right.val)) | ||
| return False |
There was a problem hiding this comment.
ほぼ同じですが、これでもいいかなと思いました。
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if root is None:
return False
stack = [(root, targetSum)]
while stack:
node, targetSum = stack.pop()
if node:
if node.left is None and node.right is None and node.val == targetSum:
return True
if node.left is not None:
stack.append((node.left, targetSum - node.val))
if node.right is not None:
stack.append((node.right, targetSum - node.val))
return False
nodchip
reviewed
Apr 28, 2026
| if root is None: | ||
| return False | ||
|
|
||
| stack = [(root, root.val)] |
There was a problem hiding this comment.
こちらのコメントをご参照ください。
rimokem/arai60#25 (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.
This problem: 112. Path Sum
Next problem: 121. Best Time to Buy and Sell Stock