Grokking 75: Top Coding Interview Questions
0% completed
Path with Maximum Sum (hard)
.....
.....
.....
Like the course? Get enrolled and start learning!
A
Anonymous
· 2 years ago
- Keep in mind that you need to check path every node. Not every leaf as it was in before problem.
- Consider if you need to sum a negative path.
A
Anonymous
· 2 years ago
lass Solution: def dfs(self, node): if not node: return 0 left = self.dfs(node.left) right = self.dfs(node.right) path = 0 if right > 0 and left < 0: path = right + node.val elif left > 0 and right < 0: path = left + node.val else: path = left + right + node.val self.max_path = max(self.max_path, path) return max(left, right) + node.val def findMaximumPathSum(self, root): self.max_path = root.val self.dfs(root) return self.max_path
A
Anonymous
· 5 years ago
what if all of the numbers in the nodes are negative?
Show 3 replies
Reading Progress
0%