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 ...
随机推荐
- 基于redis的排行榜设计和实现
前言: 最近想实现一个网页闯关游戏的排行榜设计, 相对而言需求比较简单. 秉承前厂长的训导: “做一件事之前, 先看看别人是怎么做的”. 于是乎网上搜索并参考了不少排行榜的实现机制, 很多人都推荐了r ...
- LintCode Reverse LinkedList (ArrayList 和 LinkedList 的区别)
1. ArrayList 和 LinkedList 的区别 http://pengcqu.iteye.com/blog/502676 2. How to reverse LinkedList http ...
- 论文笔记之: Person Re-Identification by Multi-Channel Parts-Based CNN with Improved Triplet Loss Function
Person Re-Identification by Multi-Channel Parts-Based CNN with Improved Triplet Loss Function CVPR 2 ...
- Sea.js学习5——Sea.js的构建工具spm
如果项目遵循推荐的标准目录结构: foo-module/ |-- dist 存放构建好的文件 |-- src 存放 js.css 等源码 | |-- foo.js | `-- style.css `- ...
- [转]UDP穿透NAT的原理与实现(UDP“打洞”原理)
NAT(The IP Network Address Translator) 的概念和意义是什么? NAT, 中文翻译为网络地址转换.具体的详细信息可以访问RFC 1631 - http://www. ...
- 如何在IIS 7.5中部署Asp.Net MVC 5的网站
0 Sign in to vote 系统是 windwos 2008 已经安装.Net 4.0 和 .Net 4.5 已经安装MVC4 的需要文件,MVC5 找不见下载地方,求各位大哥告知一下在哪里可 ...
- selenium之xpath定位和input文本
selenium之xpath定位和input文本 xpath简单定位: 打开浏览器的F12 在自己需要定位的元素的那里右键 选择copy->xpath selenium获取input下的文本: ...
- [ExtJS5学习笔记]第三节 sencha cmd学习笔记 生成应用程序构建的内部细节
本文地址: http://blog.csdn.net/sushengmiyan/article/details/38316829本文作者:sushengmiyan------------------- ...
- C# 多线程的等待所有线程结束 用 ManualResetEvent 控制
using System; using System.Collections.Generic; using System.Threading; namespace ConsoleApplication ...
- SQL:查找被锁的表,以及锁表的SQL语句(重点推荐)
--死锁检测 use master Select * --找到SPID exec sp_lock --根据SPID找到OBJID ) --根据OBJID找到表名 1.DatabaseName 同于你要 ...