LeetCode: Binary Tree Maximum Path Sum 解题报告
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.
For example:
Given the below binary tree,
1
/ \
2 3
SOLUTION 1:
计算树的最长path有2种情况:
1. 通过根的path.
(1)如果左子树从左树根到任何一个Node的path大于零,可以链到root上
(2)如果右子树从右树根到任何一个Node的path大于零,可以链到root上
2. 不通过根的path. 这个可以取左子树及右子树的path的最大值。
所以创建一个inner class:
记录2个值:
1. 本树的最大path。
2. 本树从根节点出发到任何一个节点的最大path.
注意,当root == null,以上2个值都要置为Integer_MIN_VALUE; 因为没有节点可取的时候,是不存在solution的。以免干扰递归的计算
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public class ReturnType {
int maxSingle;
int max;
ReturnType (int maxSingle, int max) {
this.max = max;
this.maxSingle = maxSingle;
}
} public int maxPathSum(TreeNode root) {
return dfs(root).max;
} public ReturnType dfs(TreeNode root) {
ReturnType ret = new ReturnType(Integer.MIN_VALUE, Integer.MIN_VALUE);
if (root == null) {
return ret;
} ReturnType left = dfs(root.left);
ReturnType right = dfs(root.right); int cross = root.val; // if any of the path of left and right is below 0, don't add it.
cross += Math.max(0, left.maxSingle);
cross += Math.max(0, right.maxSingle); // 注意,这里不可以把Math.max(left.maxSingle, right.maxSingle) 与root.val加起来,
// 会有可能越界!
int maxSingle = Math.max(left.maxSingle, right.maxSingle); // may left.maxSingle and right.maxSingle are below 0
maxSingle = Math.max(maxSingle, 0);
maxSingle += root.val; ret.maxSingle = maxSingle;
ret.max = Math.max(right.max, left.max);
ret.max = Math.max(ret.max, cross); return ret;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MaxPathSum.java
LeetCode: Binary Tree Maximum Path Sum 解题报告的更多相关文章
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [leetcode]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] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [leetcode]Binary Tree Maximum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- [LeetCode] 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. ...
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
- [LeetCode] 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. ...
- [Leetcode] 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. ...
随机推荐
- sudo: add-apt-repository: command not found
错误来啦:sudo: add-apt-repository:command not found 网上解决办法是直接安装工具包 命令:sudo apt-get install python-s ...
- PHP中使用 JKS(Java Key Store)的方法
PHP语言无法直接读取 JKS中的密钥,需要通过以下方法进行转换 本例以JKS中的私钥为例 首先 使用 KeyStore Explorer工具,打开JKS文件 ,此时可能需要输入 JKS密码 对私钥进 ...
- Redis踩过的坑
现象:在使用redis云提供的redis服务后,经常出现connect timeout: redis.clients.jedis.exceptions.JedisConnectionException ...
- spring事物要知道
事物隔离级别和传播行为: #### 1.隔离级别( isolation ) 隔离级别是指若干个并发事物之间的隔离程度,与我们开发时候主要相关的场景包括:脏读取.重复读.幻读. 我们可以看 org. ...
- Python字符串与二进制串的相互转换
python基础知识之字符编码与转换 - 机壳啦 - 博客园https://www.cnblogs.com/home979/p/7838244.html Python 字符串与二进制串的相互转换 - ...
- JetBrains PyCharm专业版激活
PyCharm最新2018激活码 激活时选择License server 填入 http://idea.imsxm.com 然后点击Active即可 PS:在线激活有一个过期时间,这个时间一过就必须再 ...
- wireshark的拆包与合并
背景:分析较高并发情景下的通话质量不佳的原因,需要长期抓包. 一.自动打包 1. 指定以1MB的大小打包,这个必须在/var/tmp/目录下执行. tcpdump -i ens32 -vvvv -C ...
- 水晶报表填充.Net Objects数据源
Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.是业内最专业.功能最强的报表系统. 查看网络资料及课本图书,鲜有介绍通过.NET Objects作为数据源 ...
- 温故而知新 Vue 原来也有this.$forceUpdate();
由于一些嵌套特别深的数据,导致数据更新了.UI没有更新(连深度监听都没有监听到),我捉摸着有没有和react一样的立即更新UI的API呢 this.forceUpdate()呢?结果还真有: this ...
- 基础002_V7-CLB
一.综述 参考ug474.pdf: 7系列中,一个CLB包含两个slice: 每个CLB的资源: CLB可配置的主要功能: 二.主要功能 LUT是基本单元,例如选择器assign muxout = ...