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:

  1. Input: [1,2,3]
  2.  
  3. 1
  4. / \
  5. 2 3
  6.  
  7. Output: 6

Example 2:

  1. Input: [-10,9,20,null,null,15,7]
  2.  
  3.   -10
  4.    / \
  5.   9  20
  6.     /  \
  7.    15   7
  8.  
  9. Output: 42
  10.  
  11. Time: O(N)
  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution:
  9. def maxPathSum(self, root: TreeNode) -> int:
  10. import sys
  11. self.res = -sys.maxsize - 1
  12. self.helper(root, self.res)
  13. return self.res
  14.  
  15. def helper(self, root, res):
  16. if root is None:
  17. return 0
  18. left = self.helper(root.left, res)
  19. right = self.helper(root.right, res)
  20. if left < 0:
  21. left = 0
  22. if right < 0:
  23. right = 0
  24. cur_max = root.val + left + right
  25. if cur_max > self.res:
  26. # resultcan choose from both children
  27. self.res = cur_max
  28. # return back only choose one path
  29. return root.val + max(left, right)
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. int res = Integer.MIN_VALUE;
  12. public int maxPathSum(TreeNode root) {
  13. helper(root);
  14. return res;
  15. }
  16.  
  17. private int helper(TreeNode root) {
  18. if (root == null) {
  19. return 0;
  20. }
  21. int left = helper(root.left);
  22. int right = helper(root.right);
  23. left = left < 0 ? 0 : left;
  24. right = right < 0 ? 0 : right;
  25. res = Math.max(res, left + right + root.val);
  26. return Math.max(left, right) + root.val;
  27. }
  28. }

[LC] 124. Binary Tree Maximum Path Sum的更多相关文章

  1. 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 ...

  2. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  3. 【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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Api_hook 拦截 messageBox 等函数

    library hookdll; uses SysUtils, Windows, Classes, unitHook in 'unitHook.pas'; {$R *.res} const HOOK_ ...

  2. java8 String intern()

    public class Solution { public static void main(String[] args) { String a = new String("he" ...

  3. kotlin黑马影音项目学习笔记

    1.包布局 --------model--------presenter----------------impl----------------interf--------view--------ui ...

  4. torch基础学习

    目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...

  5. Ubuntu的man中文包安装

    apt-get install manpages-zh vi /etc/manpath.config :,$s#/usr/share/man#/usr/share/man/zh_CN#g 第一个命令: ...

  6. Kerbernetes的Service资源管理

    Kerbernetes的Service资源管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.  一.Service概述 1>.为什么需要Service资源 我们知道在Kube ...

  7. 手机H5,用Jquery使图片自动填满两栏式排版

    遇上这样的排版,手机的解象度都不同,假如只用CSS3根本就做不出这样的排版:因此要用Jquery. 1. HTML <div class="postImgCenterCrop" ...

  8. vzray上网教程

    1.首先按照之前的教程在chrome里安装插件-Proxy-SwitchyOmega-Chromium-2.5.15 2.打开  vzray-v3.11-windows-64,打开 3.在chrome ...

  9. F5负载均衡综合实例详解(转)

    转载自:https://blog.csdn.net/weixin_43089453/article/details/87937994  女程序员就不脱发了吗来源于:<网络运维与管理>201 ...

  10. 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 ...