From 3f87455dc9d439c6aa850f98cac989a8a2f8ed88 Mon Sep 17 00:00:00 2001 From: shining-ai Date: Mon, 3 Jun 2024 01:48:16 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E3=80=90Grind75Hard=E3=80=9110=E5=95=8F?= =?UTF-8?q?=E7=9B=AE124.=20Binary=20Tree=20Maximum=20Path=20Sum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_1.py | 19 ++++++++++++++++++ .../level_2.py | 20 +++++++++++++++++++ .../level_3.py | 16 +++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 grind75_hard/10_124_Binary Tree Maximum Path Sum/level_1.py create mode 100644 grind75_hard/10_124_Binary Tree Maximum Path Sum/level_2.py create mode 100644 grind75_hard/10_124_Binary Tree Maximum Path Sum/level_3.py diff --git a/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_1.py b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_1.py new file mode 100644 index 0000000..a56d704 --- /dev/null +++ b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_1.py @@ -0,0 +1,19 @@ +# DFSを使う +# 子を2つ持てるのは根だけ +class Solution: + def maxPathSum(self, root: Optional[TreeNode]) -> int: + max_sum = -float("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 diff --git a/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_2.py b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_2.py new file mode 100644 index 0000000..72704fd --- /dev/null +++ b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_2.py @@ -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 diff --git a/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_3.py b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_3.py new file mode 100644 index 0000000..ee11bef --- /dev/null +++ b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_3.py @@ -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 From 33b53c569fd798c9b456bccdf262542a435b204f Mon Sep 17 00:00:00 2001 From: shining-ai Date: Mon, 3 Jun 2024 17:01:52 +0000 Subject: [PATCH 2/2] =?UTF-8?q?path=E3=81=AE=E6=9C=80=E5=A4=A7=E5=80=A4?= =?UTF-8?q?=E3=82=92=E5=BC=95=E6=95=B0=E3=81=A7=E3=82=84=E3=82=8A=E5=8F=96?= =?UTF-8?q?=E3=82=8A=E3=81=99=E3=82=8B=E5=BD=A2=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../level_4.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 grind75_hard/10_124_Binary Tree Maximum Path Sum/level_4.py diff --git a/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_4.py b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_4.py new file mode 100644 index 0000000..43feedd --- /dev/null +++ b/grind75_hard/10_124_Binary Tree Maximum Path Sum/level_4.py @@ -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) + 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