Skip to content

Conversation

@kazukiii
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/path-sum/description/

次に解く問題
102. Binary Tree Level Order Traversal

README.mdへ頭の中の言語化と記録をしています。

if not root.left and not root.right:
return current_sum == target_sum
if root.left:
if has_path_sum_helper(root.left, current_sum):

Choose a reason for hiding this comment

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

if root.left and has_path_sum_helper(root.left, current_sum):で書けそうです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

たしかにこちらの方がネストが浅くなっていいと思いました。

@SuperHotDogCat
Copy link

良いと思います

Copy link

@fhiyo fhiyo left a comment

Choose a reason for hiding this comment

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

良いと思います!

Comment on lines +11 to +14
targetSum -= root.val
if not root.left and not root.right:
return targetSum == 0
return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)

Choose a reason for hiding this comment

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

好みかも。自分はあまり値を動かしたくないので以下みたいに書く気がします。

if not root.left and not root.right:
  return targetSum == root.val
return self.hasPathSum(root.left, targetSum - rootl.val) or self.hasPathSum(root.right, targetSum - root.val)

# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], target_sum: int) -> bool:
def has_path_sum_helper(root: TreeNode, current_sum: int) -> bool:
Copy link

Choose a reason for hiding this comment

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

この直後に、

if not root:
    return False

としておくと、下の条件が少し減らせます。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。3つ分岐を減らせました。

class Solution:
    def hasPathSum(self, root: Optional[TreeNode], target_sum: int) -> bool:
        def has_path_sum_helper(root: TreeNode, current_sum: int) -> bool:
            if not root:
                return False
            current_sum += root.val
            if not root.left and not root.right:
                return current_sum == target_sum
            if has_path_sum_helper(root.left, current_sum):
                    return True
            if has_path_sum_helper(root.right, current_sum):
                    return True
            return False

        return has_path_sum_helper(root, 0)

Copy link

Choose a reason for hiding this comment

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

さらに

return has_path_sum_helper(root.left, current_sum) or has_path_sum_helper(root.right, current_sum)

ともできますね。

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.

6 participants