-
Notifications
You must be signed in to change notification settings - Fork 0
【Grind75Hard】10問目124. Binary Tree Maximum Path Sum #69
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?
Conversation
nodchip
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.
よいと思います。
| # 子を2つ持てるのは根だけ | ||
| class Solution: | ||
| def maxPathSum(self, root: Optional[TreeNode]) -> int: | ||
| max_sum = -float("inf") |
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.
-math.inf のほうがシンプルだと思います。
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.
leetcode上だと関係ないのですが、floatはimportなしで使えるため使用していました。
math.infを使うようにします。
| return 0, max_sum | ||
| left_sum, max_sum = helper(node.left, max_sum) | ||
| right_sum, max_sum = helper(node.right, max_sum) | ||
| max_sum = max(max_sum, node.val + left_sum + right_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.
私の想定は、分散処理を考えて下みたいなのでしたが、これでもいいです。
left_sum, left_max_sum = helper(node.left)
right_sum, right_max_sum = helper(node.right)
max_sum = max(left_max_sum, right_max_sum, node.val + left_sum + right_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.
ありがとうございます。
変数を分けた方が読みやすい気がしました。
問題
https://leetcode.com/problems/binary-tree-maximum-path-sum/