[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 ...
随机推荐
- Api_hook 拦截 messageBox 等函数
library hookdll; uses SysUtils, Windows, Classes, unitHook in 'unitHook.pas'; {$R *.res} const HOOK_ ...
- java8 String intern()
public class Solution { public static void main(String[] args) { String a = new String("he" ...
- kotlin黑马影音项目学习笔记
1.包布局 --------model--------presenter----------------impl----------------interf--------view--------ui ...
- torch基础学习
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- Ubuntu的man中文包安装
apt-get install manpages-zh vi /etc/manpath.config :,$s#/usr/share/man#/usr/share/man/zh_CN#g 第一个命令: ...
- Kerbernetes的Service资源管理
Kerbernetes的Service资源管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Service概述 1>.为什么需要Service资源 我们知道在Kube ...
- 手机H5,用Jquery使图片自动填满两栏式排版
遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...
- vzray上网教程
1.首先按照之前的教程在chrome里安装插件-Proxy-SwitchyOmega-Chromium-2.5.15 2.打开 vzray-v3.11-windows-64,打开 3.在chrome ...
- F5负载均衡综合实例详解(转)
转载自:https://blog.csdn.net/weixin_43089453/article/details/87937994 女程序员就不脱发了吗来源于:<网络运维与管理>201 ...
- PAT Advanced 1154 Vertex Coloring (25) [set,hash]
题目 A proper vertex coloring is a labeling of the graph's vertices with colors such that no two verti ...