[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. ...
随机推荐
- python 中的 print 函数与 list函数
print() 函数: 传入单个参数时默认回车换行,关键词 end 可以用来避免输出后的回车(换行), 或者以一个不同的字符串结束输出. >>> a, b = 0, 1 >& ...
- solus 系统 - 编译安裝 ibus-rime 中文输入法(附:小鹤双拼双形配置文件)
編譯方法參見官網 - https://github.com/rime/home/wiki/RimeWithIBus 安装依赖:列出几个可能用到的命令 #安裝cmake gcc等开发工具 sudo eo ...
- 强大的Android基地 论坛
强大的Android基地 论坛 [非常值得推荐,需要的自己动手去看...] 地址:http://www.eoeandroid.com/forum-15-1.html 大家论坛:http://club. ...
- java课堂动手动脑总结
java有8种基本数据类型:byte,int,short,long,boolean,char,float,double. 对应的为:Byte,Int,Short,Long,Boolean,Charec ...
- [No0000167]CPU内部组成结构及指令执行过程
计算机的基本硬件系统由运算器.控制器.存储器和输入.输出设备五大部件组成.运算器和控制器等部件被集成在一起统称为中央处理单元(Central Processing Unit,CPU). CPU的功能 ...
- [No0000CF]想有一辈子花不完的钱?从了解“被动收入”开始吧
我想从理清自己所说被动收入的含义,开始创作此被动收入系列文章. 我更喜欢把被动收入较宽泛地定义为,甚至当你没有主动工作时,仍可赚取的收益.被动收入的另一个名称是剩余收入. 相比之下,当你停止工作时,通 ...
- SQL Fundamentals || Oracle SQL语言
对于SQL语言,有两个组成部分: DML(data manipulation language) 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据 ...
- Flannel - 原理
Flannel GitHub 地址 Flannel 是 Kubernetes 中常用的网络配置工具,用于配置第三层(网络层)网络结构. 如何工作Flannel 需要在集群中的每台主机上运行一个名为 f ...
- composer命令详解
composer命令行 你已经学会了如何使用命令行界面做一些事情.本章将向你介绍所有可用的命令. 为了从命令行获得帮助信息,请运行composer或者composer list 命令,然后结合--he ...
- HZAU2018年十大阅读之星演讲稿
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...