Skip to content

Conversation

@shining-ai
Copy link
Owner

Copy link

@nodchip nodchip left a 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")
Copy link

Choose a reason for hiding this comment

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

-math.inf のほうがシンプルだと思います。

Copy link
Owner Author

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)
Copy link

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)

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。
変数を分けた方が読みやすい気がしました。

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