Maximum Path In Between Any Node Of Binary Tree | Using Pair and Static Method | Leetcode 124
ฝัง
- เผยแพร่เมื่อ 19 ก.ย. 2024
- Please consume this content on nados.pepcoding.com for a richer experience. It is necessary to solve the questions while watching videos, nados.pepcoding.com enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. For a better experience and more exercises, VISIT: www.pepcoding....
Have a look at our result: www.pepcoding....
Follow us on our TH-cam page: / pepcoding
Follow us on our FB page: / pepcoding
Follow us on Instagram: / pepcoding
Follow us on LinkedIn: / pepcoding-education
Follow us on Pinterest: / _created
Follow us on Twitter: home
.
.
.
Happy Programming !!! Pep it up 😍🤩
.
.
.
#pepcoding #code #coder #codinglife #programming #coding #java #freeresources #datastrucutres #pepcode #competitive #competitiveprogramming #softwareengineer #engineering #engineer
Sounds like Kadane!
You are really awesome sir!! You cleared every doubt related to bt (specially in java) tysm sir❤️
it is like we are applying kadane on binary tree
Awesome sir!!!
thanks sir!!
Pending videos :
Root to all leaf path in binary tree
Path sum in binary tree
Path sum in binary tree 2
Please upload these before uploading diameter of binary tree all methods
This is not working for the test case when all values are negative
For better experience and well-arranged content
visit- nados.pepcoding.com
Also you can post your query on community tab.
Don't forget to follow us on Instagram
instagram.com/pepcoding/
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
self.max=-float("inf")
self.dfs(root)
return self.max
def dfs(self,root):
if not root:
return 0
l = self.dfs(root.left)
r = self.dfs(root.right)
op1 = root.val+max(l,r,0)
op2 = root.val+l+r
self.max=max(self.max,op1,op2)
return op1