Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions grind75_hard/10_124_Binary Tree Maximum Path Sum/level_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# DFSを使う
# 子を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を使うようにします。


def helper(node):
nonlocal max_sum
if not node:
return 0
left_sum = helper(node.left)
right_sum = helper(node.right)
one_child_val = max(node.val + left_sum, node.val + right_sum)
two_child_val = node.val + left_sum + right_sum
max_sum = max(max_sum, node.val, one_child_val, two_child_val)
return max(node.val, one_child_val)

helper(root)
return max_sum
20 changes: 20 additions & 0 deletions grind75_hard/10_124_Binary Tree Maximum Path Sum/level_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 子ノードが負の場合は、その子ノードを選ばない(0にする)ように修正
# root.valを初期値にしておく
# 一応rootがNoneの場合は0を返すようにする
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
max_sum = root.val

def helper(node):
nonlocal max_sum
if not node:
return 0
left_sum = max(0, helper(node.left))
right_sum = max(0, helper(node.right))
max_sum = max(max_sum, node.val + left_sum + right_sum)
return node.val + max(left_sum, right_sum)

helper(root)
return max_sum
16 changes: 16 additions & 0 deletions grind75_hard/10_124_Binary Tree Maximum Path Sum/level_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
max_sum = root.val

def helper(node):
nonlocal max_sum
if not node:
return 0
left_sum = max(0, helper(node.left))
right_sum = max(0, helper(node.right))
max_sum = max(max_sum, node.val + left_sum + right_sum)
return max(node.val + left_sum, node.val + right_sum)

helper(root)
return max_sum
15 changes: 15 additions & 0 deletions grind75_hard/10_124_Binary Tree Maximum Path Sum/level_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 最大値を引数で渡すように修正
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:

def helper(node, max_sum):
if not node:
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.

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

max_one_child = node.val + max(left_sum, right_sum)
return max(0, max_one_child), max_sum

_, max_sum = helper(root, root.val)
return max_sum