Skip to content

112. Path Sum#19

Open
hiro111208 wants to merge 1 commit into
mainfrom
112-path-sum
Open

112. Path Sum#19
hiro111208 wants to merge 1 commit into
mainfrom
112-path-sum

Conversation

@hiro111208
Copy link
Copy Markdown
Owner

Comment thread step2.md
nodes_and_sums = [(root, root.val)]
while nodes_and_sums:
node, current_sum = nodes_and_sums.pop()
if node:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必要なさそうですかね?

必要だとしても、ネストを一段減らすために、if not node: continueの形にした方が良さそうです。

また、Noneかどうかのチェックの方法に一貫性がないのが気になります。

Comment thread step2.md
Comment on lines +21 to +22
if node.left is None and node.right is None and current_sum == targetSum:
return True
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_sum == targetSumのチェックはifの中に入れて、一致しないならcontinueする方が良いと思います。

Comment thread README.md
@@ -1,6 +1,6 @@
# LeetCode

## 問題: [Link Text](URL)
Copy link
Copy Markdown

@liquo-rice liquo-rice Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRをマージしないにしても、テンプレフォルダーをコピーして、問題ごとにフォルダーを作ると、diffが綺麗に出て、(マージするなら)ローカルにファイルも残るので良いと思います。

Comment thread step1.md
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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ほぼ同じですが、これでもいいかなと思いました。

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

Comment thread step1.md
if root is None:
return False

stack = [(root, root.val)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらのコメントをご参照ください。
rimokem/arai60#25 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants