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.
算法:(有点动态规划的思想)首先明确要自底向上进行计算,只考虑当前节点root,我们从子节点向上返回的是一条线路目前的最大值,该线路最高层(最上面)的节点是该子节点,并且该子节点不能同时有左右分支(即在线路上不能同时有左右分支,如果有分支,root返回到上一节点后,不能形成一个链,会在root出现分叉),然后我们比较root->val,
root->val+leftMax(从左子树返回的值),root->val+rightMax(从右子树返回的值),其中最大的就是root节点应该向上返回的值,我们在计算root节点的返回值时,可以顺便计算以root为最高层节点的链的线路最大值(可以有分叉),只要比较,root->val, leftMax+root->val, root->val+rightMax, leftMax+rightMax+root->val,其中最大的就是以root为最高层次节点的线路的最大值,然后用这个最大值,和最初保存的最大值result(初始化为INT_MIN)做比较,如果大于result,更新result,最终result就是结果,代码如下,时间复杂度O(n),只需要遍历一边二叉树(后序遍历):
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int result;
int maxPathSum(TreeNode *root) {
result=INT_MIN;
getMax(root);
return result;
}
int getMax(TreeNode* root)
{
if(root==NULL) return ;
int leftMax=getMax(root->left);
int rightMax=getMax(root->right);
int tmax=max(max(leftMax+root->val,max(rightMax+root->val,root->val)),leftMax+rightMax+root->val);
if(tmax>result) result=tmax;
int rootmax=max(root->val,max(root->val+leftMax,root->val+rightMax));
return rootmax;
}
};
Binary Tree Maximum Path Sum的更多相关文章
- [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 and ...
- 26. 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 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: 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】124. 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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [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(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
随机推荐
- Phaser开源2d引擎 html5游戏框架中文简介
功能特点(Features) 易维护代码(Easy Asset Loading) Phaser可以加载图片,音频文件,数据文件,文本文件和自动解析精灵图和纹理地图集数据(出口纹理封隔器或Flash C ...
- C++ 中的对象布局
C++中的涉及到虚表时,类对象的布局分为:虚表与数据成员,子类包含派生类布局,假设下面一个程序: #include <iostream> using namespace std; clas ...
- 收集的maven 仓库地址(maven repository)
maven 仓库地址: 共有的仓库http://repo1.maven.org/maven2/http://repository.jboss.com/maven2/http://repository. ...
- 学习使用monkey 测试
一.Monkey测试简介Monkey测试是Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程序进行压力测试,检测程序多久的时 ...
- For xml path
Select * from tb for xml path('') 特点: 1. 以xml形式展示查询数据. 2. 自定义数据展示类型. 实例: 1. Select * from tb for xml ...
- mysql事务,START TRANSACTION, COMMIT和ROLLBACK,SET AUTOCOMMIT语法
http://yulei568.blog.163.com/blog/static/135886720071012444422/ MyISAM不支持 START TRANSACTION | BEGIN ...
- 解决Maven项目编译时提示:源值1.5已过时,将在未来所有版本中删除
每次编译项目时,都提示:源值1.5已过时,将在未来所有版本中删除 查了一些资料,发现是因为IDEA默认把项目的源代码版本设置为jdk1.5,目标代码设置为jdk1.5 解决方案: 修改Maven的S ...
- 一个服务器上面配置多个IP ,实现指定IP的域名请求
//配置多个IP命名using System.Net; //********************************************************************** ...
- hadoop实战之分布式模式
环境 192.168.1.101 host101 192.168.1.102 host102 1.安装配置host101 [root@host101 ~]# cat /etc/hosts |grep ...
- PHP中的Libevent学习
wangbin@2012,1,3 目录 Libevent在php中的应用学习 1. Libevent介绍 2. 为什么要学习libevent 3. Php libeven ...