[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
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的更多相关文章
- 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 求二叉树的最大路径和
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 (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 ...
- 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 ...
- 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 ...
- 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. ...
随机推荐
- F - Unix ls
The computer company you work for is introducing a brand new computer line and is developing a new U ...
- ssm文件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Android性能测试--内存
前言: 近阶段都在探索android性能测试方面的东西,其中一个很重要的指标就是内存.对于内存,主要是一些gc是不是及时,或者说一些引用有没有及时释放,有没有导致oom或者内存持续增加导致卡顿,有没有 ...
- thinkphp 点击某个class提交post值,返回回来用一个弹窗插件,提示返回来要说的话
下一篇文章有讲到弹窗插件的怎么使用,自写教程 如果能帮到你,给点个赞鼓励一下 <============= 控制器 =================> public function ...
- Zabbix使用grafana展示图形
系统环境查看 官网下载grafana wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.0.1-1. ...
- myEclipse中项目无法部署到tomcat
问题现象: 从svn上新下载了项目到win环境上. 部署项目的时候,在servers视图里,Add Deployment,如下图: 发现只有一个项目可以加载,另外的项目看不到:可是明明我并没有部署过啊 ...
- hdu5157 Harry and magic string【manacher】
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 简单的Git流程
一.安装cygwin 略 二. 1.创建一个文件 $echo "中华人民共和国">>abc.txt 2.添加到文件追踪 $git add abc.txt 上一行是提交一 ...
- 10.11JAVA作业
[实验任务一]:素数输出 1. 实验要求: (1)编写判断该数是否为素数的方法,并在主方法中对其进行调用. (2)注意编程规范:程序开头部分的目的,作者以及日期:必要的空格与缩进,适当的注释等: (3 ...
- 3-idiots hdu4609 母函数+FFT 组合数学题
http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:1e5个数,求取三个数能形成三角形的概率. 题解(这怎么会是fft入门题QAQ): 概率的算法就是三 ...