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 这个题目的思路就是用DFS, 然后我们建一个helper function(input: root, 得到从这个root出发的一条path s.t path sum 最大), 得到包括root的最大的path sum,
然后recursive 去看是否包括root.left path sum 和root.right path sum, 这里需要
注意的是, 返回的时候因为是path, 所以left和right只能选一边, 只是self.ans 可以考虑left + right+ root.最后返回self.ans 04/18/2019 update
可以一步一步的来看, 第一步, 考虑 root -> leaf 的maximum sum,利用Divide and conquer
class Solution:
def maxSum(self, root):
if not root: return 0
left = self.maxSum(root.left)
right = self.maxSum(root.right)
return root.val + max(left, right)

第二步, 考虑 root -> any node 的maximum sum, 实际上就是基于root -> leaf的情况下,加入了如果children 的maximum sum 是负数的情况,那么就不要加入children的maximum sum。

class Solution:
def maxSum(self, root):
if not root: return 0
left = self.maxSum(root.left)
right = self.maxSum(root.right)
return root.val + max(0, left, right)

最后考虑any node -> any node的情况, 就可以每个node, 都判断以该点作为root的maximum sum,也就是上面的第二步,只不过设置一个全局变量去将所有点的maximum sum,以及该点加上左,右之后的sum,取最大值,就是我们要的结果。


1. Constraints
1) empty => 0 update:题目说了non-empty,所以不会有。 2. Ideas
DFS T: O(n) S: O(n) 3. Code
 class Solution:
def maxSumPath(self, root):
self.ans = None
def rootSum(root):
if not root: return 0
left, right = rootSum(root.left), rootSum(root.right)
localSum = max(root.val, root.val + left, root.val + right)
potentialSum = max(localSum , root.val + left + right)
if self.ans == None or potentialSum > self.ans:
self.ans = potentialSum
return localSum
if not root: return 0 # since it is non-empty tree, so this is useless
rootSum(root)
return self.ans

4. Test cases

1) empty => 0 , leetcode上面好像这里返回的是Min_value, 一个最小负数, 我觉得应该是0, 不过test cases里面没有加, 所以无所谓  update:题目说了non-empty,所以不会有。

2) -1

3)

   -10
   / \
  9  20
    /  \
   15   7

[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer的更多相关文章

  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 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

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

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

  6. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

    题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...

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

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

  9. Java for LeetCode 124 Binary Tree Maximum Path Sum

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. hadoop Codec

  2. 洛谷P1101 单词方阵【暴力】【字符串】

    题目描述 给一n×nn \times nn×n的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 888 个方向的任一方向,同一单词摆放时不再改变方向, ...

  3. redis集群,主从,持久化

    1,单机版 先安装gcc   yum install gcc-c++ 然后解压源码包,执行编译命令make(C语言写的,需要gcc环境),最后安装Redis,需要通过PREFIX指定安装路径make ...

  4. gulp安装及使用流程

    1. 安装nodejs 去 https://nodejs.org/en/下载安装文件安装即可. 安装完成后,在终端输入node -v回车打印出nodejs的版本号,说明nodejs安装成功. 在终端输 ...

  5. 洛谷试炼场-简单数学问题-P1045 麦森数-高精度快速幂

    洛谷试炼场-简单数学问题 B--P1045 麦森数 Description 形如2^P−1的素数称为麦森数,这时P一定也是个素数.但反过来不一定,即如果PP是个素数,2^P-1 不一定也是素数.到19 ...

  6. [No0000143]Win10“卓越性能模式”

    从不久之前Win10更新1803以来,微软不仅带来了一些新功能(和BUG),还悄悄地加入了一个“卓越性能模式”,老张想了想,不对呀,以前就有了一个“高性能模式”,这怎么就还多出个新的性能模式来,难道会 ...

  7. [No0000DB]C# FtpClientHelper Ftp客户端上传下载重命名 类封装

    using System; using System.Diagnostics; using System.IO; using System.Text; using Shared; namespace ...

  8. hide server info

    <?php /*wamp64\bin\apache\apache2.4.18\confhttpd.conf ServerSignature On  ServerTokens Full Serve ...

  9. ssm,dubbo框架搭建得配置文件

    在建立了parent,common,manager(pojo,mapper,sercie,web)的maven工程后,开始导入添加配置文件: pojo,mapper 最终会打成jar包,service ...

  10. 使用XPath对象解析xml文件

    使用XPath对象解析xml文件 1.DocumentBuilderFactory类  工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器 其构造方法受保护,用newInstance() ...