From eac1022606c1eee376e307cab36547bf2367d981 Mon Sep 17 00:00:00 2001 From: Shekhar Chaugule <79699567+shekhargit1912@users.noreply.github.com> Date: Mon, 12 Dec 2022 01:33:06 +0530 Subject: [PATCH] Binary Tree Maximum Path Sum in python language --- .../Python/Binary Tree Maximum Path Sum.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Leetcode Challenge/december2k22/Python/Binary Tree Maximum Path Sum.py diff --git a/Leetcode Challenge/december2k22/Python/Binary Tree Maximum Path Sum.py b/Leetcode Challenge/december2k22/Python/Binary Tree Maximum Path Sum.py new file mode 100644 index 0000000..7820cfa --- /dev/null +++ b/Leetcode Challenge/december2k22/Python/Binary Tree Maximum Path Sum.py @@ -0,0 +1,33 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def maxPathSum(self, root: Optional[TreeNode]) -> int: + self.maxi=-99999 + + def maxpath(root): + + if root is None: + return 0 + + + lh=max(0,maxpath(root.left)) + rh=max(0,maxpath(root.right)) + + self.maxi=max(self.maxi,root.val+lh+rh) + + + + + + + return root.val+max(lh,rh) + + + + + maxpath(root) + return self.maxi