[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解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- 1、C#基础:变量、运算符、分支、循环、枚举、数组、方法 <转>
转自:海盗船长 链接:http://www.cnblogs.com/baidawei/p/4701504.html#3241882 C#..Net以及IDE简介 一.什么是.Net? .Net指 .N ...
- 简单的ATM,见笑见笑
#include<stdio.h>#include<string.h> void main(){ char a[4]={0}; char e[]="9527&quo ...
- Fragment之间的通信(四)
自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...
- 使用crosswalk优化ionic2应用包
ionic plugin add cordova-plugin-crosswalk-webview --save
- C语言程序设计第8堂作业
一.本次课主要内容: 本次课通过以下两个知识点来完成: (1)以数字金字塔为例,介绍函数的另一种形式,即不返回结果的函数.不返回结果的函数在定义.调用.参数传递.函数声明上,思路完全与以前相同,只是函 ...
- Axis2测试webservice server以及client
一.环境搭建 下载axis2-1.6.2-war.zip/axis2-1.6.2-bin.zip等. 参考axis2-1.6.2-war\README.txt以及axis2-1.6.2-war\axi ...
- alpha值的问题
但凡图像都会涉及到透明度问题.使用透明度之后就可以看到多层图像.Alpha值就是用于描述透明度的参量.Alpha值是一个百分数,alpha=1表示源文件发出的光全部被观察者观察到. 既然是透明度,那么 ...
- SqlServer性能优化 提高并发性能(八)
并发访问: 当多个线程访问同一个资源,会产生并发性问题 并发控制与处理: 乐观并发控制:一种方式是"后来的更新者获胜" 这意味着先来的用户提交的值会在没有察觉的情况下丢失. 为 ...
- 黑马----JAVA比较器:Comparable和Comparator
黑马程序员:Java培训.Android培训.iOS培训..Net培训 一.Comparable接口 1.public interface Comparable{ public int compare ...
- python小细节
1.tab缩进2.读取文件,在文件名前加r或者R.这是因为python原始字符串特性,即在字符串的前面已R或者小写字母r开始,则字符串不对\进行转移,直接输出,通常用于表示windows的路径.fil ...