[LC] 124. Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7] -10
/ \
9 20
/ \
15 7 Output: 42 Time: O(N)
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def maxPathSum(self, root: TreeNode) -> int:
import sys
self.res = -sys.maxsize - 1
self.helper(root, self.res)
return self.res def helper(self, root, res):
if root is None:
return 0
left = self.helper(root.left, res)
right = self.helper(root.right, res)
if left < 0:
left = 0
if right < 0:
right = 0
cur_max = root.val + left + right
if cur_max > self.res:
# resultcan choose from both children
self.res = cur_max
# return back only choose one path
return root.val + max(left, right)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
helper(root);
return res;
} private int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
left = left < 0 ? 0 : left;
right = right < 0 ? 0 : right;
res = Math.max(res, left + right + root.val);
return Math.max(left, right) + root.val;
}
}
[LC] 124. Binary Tree Maximum Path Sum的更多相关文章
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【LeetCode】124. Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode 124. Binary Tree Maximum Path Sum ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- 证书打印CSS知识点总结
需求: 1.证书内容动态填充: 2.证书背景图不要求打印,只为展示作用: 3.打印内容兼容屏幕分辨率: 实现: <!-- 外层div宽度为背景图片宽 --> <div style=& ...
- ssh首次链接出现yes/no提示和ansible提示
修改文件: /etc/ssh/ssh_config 在文件中添加如下信息:StrictHostKeyChecking no 改本机的/etc/ssh/ssh_config文件中的"# Str ...
- Coursera机器学习——Recommender System测验
第一题本应该是基础题,考察Cost Function不同形式的表示方法,但却难住了我,说明基本概念掌握不够到位. 1. 在求和的部分,有两种可能,一种是(i,j)同时求和,即∑(i,j):r(i,j) ...
- 化 Bernoulli 方程为一阶线性微分方程
形如 $ {\displaystyle \frac{dy}{dx}+p(x)y=q(x)y^n(n\neq 0,1) \ \ \ \ \ (1)}$ 的方程为 Bernoulli 方程.现在我们考虑其 ...
- Spring Cloud Alibaba 教程 | Nacos(六)
集群模式部署 前面我们已经学习了Nacos作为注册中心.配置中心的相关功能,但是我们之前启动Nacos是通过单实例模式启动的,只适合在学习和开发阶段,生产环境需要保证Nacos的高可用,所以今天我们来 ...
- dozer
1.简介 dozer是用来两个对象之间属性转换的工具,有了这个工具之后,我们将一个对象的所有属性值转给另一个对象时,就不需要再去写重复的set和get方法了. 2.如果两个类之间的属性有些属性意思一样 ...
- 基于Token的身份验证
最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...
- 201503-1 图像旋转 Java
思路: 观察输入和输出可以发现,第三列输出为第一行,第二列输出为第二行,第一列输出为第三行.循环即可 import java.util.Scanner; //得分80,本题最高需要输入100W次,因为 ...
- Gradle project sync failed. Please fix your project and try again
https://stackoverflow.com/questions/29808199/error-running-android-gradle-project-sync-failed-please ...
- android implementation 依赖第三方库
依赖第三方库