[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 ...
随机推荐
- winform操作windows系统计算器
winform对系统计算器的调用,启动,最大化最小化显示,在mainwindow设置topmost=true时,正常显示计算器并置顶. /// <summary> /// 获取窗体的句柄函 ...
- PAT Advanced 1079 Total Sales of Supply Chain (25) [DFS,BFS,树的遍历]
题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone in ...
- Mybatis之一级缓存(七)
1. 介绍 Mybatis缓存分为一级缓存和二级缓存,在本节中我们介绍下一级缓存的使用及其特性 MyBatis的一级缓存是在一个Session域内有效的,当Session关闭后,缓存内容也随之销毁.缓 ...
- JAVE官方文档
官网地址:http://www.sauronsoftware.it/projects/jave/manual.php JAVE manual Installation and requirements ...
- SEO初步学习之影响网站排名的因素
本文介绍一些比较明显的因素,一些隐藏较深的原因还有待发掘: 1.采集网站内容,即抄袭其他网站的内容. 2.新站上传后建议不要有大的改动. 3.标题频繁修改. 4.大量投放垃圾外链. 5.不做友链,交友 ...
- awk 中 RS,ORS,FS,OFS 区别与联系
一,RS与ORS 1,RS是记录分隔符,默认的分隔符是\n,具体用法看下 [root@krlcgcms01 mytest]# cat test1 //测试文件 111 222 333 444 ...
- svg用例
圆<circle cx="x" cy="y" r="r" style="stroke:black;fill:none&quo ...
- Ubuntu 安装软件时显示:无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用)
出错状况:在用 sudo apt-get install 安装软件时,结果终端提示: 无法获得锁 /var/lib/dpkg/lock -open(资源暂时不可用) 无法锁定管理目录(var/lib/ ...
- keras猫狗大战
先划分数据集程序训练集中猫狗各12500张现在提取1000张做为训练集,500张作为测试集,500张作为验证集: # -*- coding: utf-8 -*-import os, shutil or ...
- swoole使用协程
协程:协程可以理解为纯用户态的线程,其通过协作而不是抢占来进行切换.相对于进程或者线程,协程所有的操作都可以在用户态完成,创建和切换的消耗更低.Swoole可以为每一个请求创建对应的协程,根据IO的状 ...