aasimon@uc.cl
BAN USER
Comments (3)
Reputation 30
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
class Solution(object):
def maxPathSum(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = float('-inf')
self.dfs(root)
return self.res
def dfs(self, node):
if not node: return 0
l = self.dfs(node.left)
if l < 0: l = 0
r = self.dfs(node.right)
if r < 0: r = 0
if l+r+node.val > self.res:
self.res = l+r+node.val
return node.val + max(l,r)
Comment hidden because of low score. Click to expand.
0
of 0 vote
Average is easy, because you can just use math to compute the new average, no need to use all the previous elements, just use your current average to get the new one.
Calculate the median is more difficult though.
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
average' = average + (new_value - average)/(n+1)
- aasimon@uc.cl August 11, 2018