[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
Return 6.
递归求解。
maxPathSum(root)跟maxPathSum(root.left)和maxPathSum(root.right)之间的关系:
root左子树的maxPath,右子树的maxPath以及根节点之间无法建立直接递归关系。也就是说以下的递推式不成立:
maxPathSum(root) = max{ maxPathSum(root.left), maxPathSum(root.right), maxPathSum(root.left) + maxPathSum(root.right) + root.val }
然而,按照动态规划的思路,root的结果跟其左右子树的结果之间应该是存在递推关系的:
maxPathSum(root) = F( maxPathSum(root.left), maxPathSum(root.right), root )
只是,这个root节点加进来之后如何影响最优解?进一步梳理思路:
F( maxPathSum(root.left), maxPathSum(root.right), root )
/ max{maxPathSum(root.left), maxPathSum(root.right)}, if root 将不包含在最长路径中
= {
\ max{maxPathSum(root.left), maxPathSum(root.right), max path sum of the path includes root}, if root 将包含在最长路径中
问题将归结为:求出一条包含root节点的最长路径,并比较该路径的长度与其左右子树的最长路径长度。
所以,在递归过程中,我们需要计算两个值:
1)包含节点在内的最长路径(只可能跟该节点的左子树或者右子树相关);
2)该节点作为根节点的子树的最长路径和;
定义class描述这个递归中间结果:
class max_val {
int max_path_include_root_half_tree; // 辅助值
int max_path_sum; // 待求解值
public void set(int x) {
max_path_include_root_half_tree = max_path_sum = x;
}
}
递归过程:
private void maxPathSum(TreeNode root, max_val max_vs) {
if (root == null) {
max_vs.set(-2147483647 >> 2);
return;
}
if (root.left == null && root.right == null) {
max_vs.set(root.val);
return;
}
max_val left_ = new max_val();
maxPathSum(root.left, left_);
max_val right_ = new max_val();
maxPathSum(root.right, right_);
int a = left_.max_path_include_root_half_tree + root.val;
int b = right_.max_path_include_root_half_tree + root.val;
int c = root.val;//a,b,c中包含root的值,并且最多包含了左子树或者右子树,这类路径可用于组成包含父节点的路径
int d = a + right_.max_path_include_root_half_tree;
int f = left_.max_path_sum;
int g = right_.max_path_sum;
max_vs.max_path_include_root_half_tree = max(new int[] { a, b, c });// 包含root的最长路径
max_vs.max_path_sum = max(new int[] { a, b, c, d, f, g });// root作为根节点的树的最长路径和
}
最终求解:
public int maxPathSum(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
max_val mv_l = new max_val();
maxPathSum(root, mv_l);
return mv_l.max_path_sum;
}
[leetcode]Binary Tree Maximum Path Sum的更多相关文章
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 二叉树系列 - 二叉树里的最长路径 例 [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 求二叉树的最大路径和
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 @ 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 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- 006 复杂的数据类型&函数(方法)
2016-01-16 1.变量类型int double string char bool decimal变量的使用规则:先声明再赋值最后使用int number;number=10; number=2 ...
- Session阻塞 读写锁引发的小问题
引子 我们来看两个ajax方法,先后的问题,会有什么样的结果? Javascript: $(function () { //第一个 $.ajax({ type: "POST&qu ...
- 遗传算法的C语言实现(一):以非线性函数求极值为例
以前搞数学建模的时候,研究过(其实也不算是研究,只是大概了解)一些人工智能算法,比如前面已经说过的粒子群算法(PSO),还有著名的遗传算法(GA),模拟退火算法(SA),蚁群算法(ACA)等.当时懂得 ...
- 1Caesar加密
Julius Caesar发明的较早的加密术,举个例子: 明文: meet me after the toga party 密文: PHHW PH DIWHU WKH WRJD SDUWB 其实就 ...
- bootstraps字体图标无法显示
使用bootstraps字体图标,必须在css的同级文件夹下,建立新的文件夹为fonts,放入一下文件. 在还是无法显示字体图标的情况下,可查看bootstraps.css中的 @font-face ...
- mysql启动,关闭,重启
1)启动: ? 1 sudo /etc/init.d/mysql start 2)停止: ? 1 sudo /etc/init.d/mysql stop 3)重启: ? 1 sudo /etc/ini ...
- opensuse 13.1 安装配置从0开始
主要目的为自己留作备份,仅作参考! 1. 输入法 托盘->输入法->配置 去掉除英语和Sunpinyin之外的输入法,配置Sunpinyin,使用双拼方案,重启fcitx. 另外需要禁用笔 ...
- vld使用
1.下载VLD官方版本 2.安装 3.在vs里面的属性里->c/c++->常规->副含附加目录 C:\Program Files (x86)\Visual Leak Detecto ...
- HDU 5441 离线处理 + 并查集
题意:给n个节点m条带权值边的无向图.然后q个问题,每次询问点对的数目,点对需要满足的条件是:1)连通:2)其路径的最大权值不能超过询问值. 分析:如果没次询问一次,dfs一次,很可能超时,因此可以用 ...
- 浅谈js的事件冒泡机制
很多人都听说过,js的事件冒泡机制,其实,这个说法还是比较生动形象的,就是一个水泡在水底下,冒泡到水面的过程. 那js的事件冒泡机制呢,就是一个DOM树,一级一级向上冒的过程,最终是到document ...