124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6
.
=============
题目:树中联通路径中,路径元素和最大值?
路径利用父子关系相连,不必经过根节点root
路径可以从任意节点开始,到任意节点.
=====
思路:
这道题的解法,来自网络leetcode150题集合,
利用dfs遍历树的方式,传递每个树父子节点间的路径大小;
利用一个全局遍历来时记住max_sum来记录已经找到的最大的路径值,
最难理解的是怎么在 dfs遍历树的过程,传递路径信息?
-----
借用"最大连续子序列和"问题的思路,array只有一个方向,
但是Tree有左右两个方向,我们需要比较两个方向上的值.
先算出左右子树的结果L和R.
如果L>0,那么对后序结果是有利的,后序中加上L,(不是向max_sum中,而是向如节点中)
如果R>0,那么对后序结果是有利的,加上R
---
==========
代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
///
int help_maxPathSum(int &max_sum,TreeNode *root){
if(root==nullptr) return ;
int l = help_maxPathSum(max_sum,root->left);
int r = help_maxPathSum(max_sum,root->right);
int sum = root->val;
if(l>) sum += l;
if(r>) sum += r;
max_sum = max(max_sum,sum);
return max(r,l)>? max(r,l)+root->val:root->val;//只能向上传递 左子树或者 右子树的有利值
}
int maxPathSum(TreeNode* root) {
int max_sum = INT_MIN;
help_maxPathSum(max_sum,root);
return max_sum;
}
};
124. Binary Tree Maximum Path Sum的更多相关文章
- 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 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【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] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 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 sequence ...
- leetcode 124. Binary Tree Maximum Path Sum ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 124. Binary Tree Maximum Path Sum *HARD* -- 二叉树中节点和最大的路径的节点和
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
随机推荐
- 关于 Ajax 提交参数格式,及返回类型json
function Login() { $.ajax({ //提交方式 type:&q ...
- 图像处理之face morphing
以前在论坛.微博经常看到一张脸,五官长得像A,脸型似乎又是B,觉得很有意思. 比如像这张图片.这张图片应该是网友用Photoshop完成的,他们取了郭大爷的五官,放在金元帅的脸上,在把边缘处理平滑. ...
- scala言语基础学习八
- java中将汉字转换成16进制
技术交流群:233513714 /** * 将汉字转换车16进制字符串 * @param str * @return st */ public static String enUnicode(Stri ...
- Bootloader的原理以及实现(转载)
BootLoader工作原理 BootLoader工作原理 BootLoader指系统启动后,在操作系统内核运行之前运行的一段小程序.通过BootLoader,我们可以初始化硬件设备.建立内存空间的映 ...
- Doing well in your courses ---- a guide by Andrej Karpathy
Doing well in your courses a guide by Andrej Karpathy Here is some advice I would give to younger st ...
- Intel MKL函数,如何得到相同的计算结果?【转】
在运行程序时,我们总希望多次运行的结果,是完全一致,甚至在不同的机器与不同的OS中,程序运行的结果每一位都完全相同. 事实上,程序往往很难保证做到这一点. 为什么呢? 我们先看一个简单的例子: 当程序 ...
- ruby杂记
ruby基本类中的方法:puts Object.private_instance_methods
- 部署 mozilla-BrowserQuest
1,到GitHub下载代码 https://github.com/mozilla/BrowserQuest 2,安装Node.Js 下载地址 http://nodejs.org/ 直接下载安装版就可 ...
- noip2003复赛普及组第一题——乒乓球
/*======================================================================= 题一.乒乓球(Table.pas) [问题背景]国际 ...