-
Notifications
You must be signed in to change notification settings - Fork 0
112. Path Sum #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
112. Path Sum #26
Conversation
| 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): |
There was a problem hiding this comment.
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):で書けそうです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
たしかにこちらの方がネストが浅くなっていいと思いました。
|
良いと思います |
fhiyo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
良いと思います!
| 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) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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としておくと、下の条件が少し減らせます。
There was a problem hiding this comment.
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)There was a problem hiding this comment.
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)ともできますね。
問題へのリンク
https://leetcode.com/problems/path-sum/description/
次に解く問題
102. Binary Tree Level Order Traversal
README.mdへ頭の中の言語化と記録をしています。