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 ...
随机推荐
- linux信号处理及libcurl的坑
前言: 最近有个项目, 需要访问第三方服务. 该服务是通过http的形式访问的, 为了安全和加密, 对方提供了一个加密用的C/C++库, 用于对参数进行处理. 鉴于此, 选用了C/C++语言 ...
- C#裁剪照片并保存
/// <summary> /// /// </summary> /// <summary> /// 生成缩略图 /// </ ...
- makefile--统一目标输出目录 (六)
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 上一节我们把规则单独提取出来,方便了Makefile的维护,每个模块只需要给出关于自己的一些变量 ...
- svn 安装 、使用(2)
写在前面的话: p.s.有必要读一读,不然可能会浪费你的时间. 该篇是接着上一篇的,上一篇是看了很多人写的文章,汇总的一些可能的情况,最后还是没有成功.此篇是在一个同学的帮助下成功的,也是在自己做好的 ...
- 错误:无法访问android.app.Activity 找不到android.app.Activity的类文件
视频里面在工程ndk22/bin/classes中 运行javah com.cn.ndk22.Ndk22.Activity ,出现了.h文件 但是我在bin/classes目录中运行javah 时出 ...
- java多线程之:SynchronousQueue队列
SynchronousQueue是这样一种阻塞队列,其中每个 put 必须等待一个 take,反之亦然.同步队列没有任何内部容量,甚至连一个队列的容量都没有. 不能在同步队列上进行 peek ...
- 复利计算器4.0 【java版】
import java.util.Scanner; public class FuLi { public static void main(String[] args) { ; Scanner sca ...
- C++ 遇到的问题小结
1. cannot convert 'std::basic_string<char>' to 'int' in assignment ... 原始code如下: int id2; std: ...
- CUDA学习笔记(二)【转】
来源:http://luofl1992.is-programmer.com/posts/38847.html 编程语言的特点是要实践,实践多了才有经验.很多东西书本上讲得不慎清楚,不妨自己用代码实现一 ...
- PHP-网页跳转的几种方式
本文总结了跳转到指定网页的几种方式. 1.利用PHP的header函数Location响应头, header是用来向浏览器返回HTTP响应头(详细请看HTTP协议详解) <?php header ...