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的更多相关文章

  1. 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 ...

  2. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  3. 【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 ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 完全搞懂傅里叶变换和小波(1)——总纲<转载>

    无论是学习信号处理,还是做图像.音视频处理方面的研究,你永远避不开的一个内容,就是傅里叶变换和小波.但是这两个东西其实并不容易弄懂,或者说其实是非常抽象和晦涩的! 完全搞懂傅里叶变换和小波,你至少需要 ...

  2. 标准盒模型与ie盒模型

    ff(标准的盒模型) Box的宽高包括 padding .border.margin.content区域 ie Box的宽度包括  margin  content区域(content区域包含paddi ...

  3. boot/setup.S

    !!    setup.S        Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...

  4. poj3249 Test for Job ——拓扑+DP

    link:http://poj.org/problem?id=3249 在拓扑排序的过程中进行状态转移,dp[i]表示从起点到 i 这个点所得到的的最大值.比如从u点到v点,dp[v]=max(dp[ ...

  5. Android 中解析 JSON

    有什么不懂的可以去官网去看看:www.json.org 在google android中也有关于解析JSON的类库:JsonReader,但是只能在3.0以后的版本中才可以用,在这里我们用google ...

  6. IE6 7 8BUG锦集

    1.浮动元素的双倍margin 说明:这是IE6及其以下版本的一个经典的BUG,触发这个BUG产生的条件是给元素设置了浮动并且同一方向设置了margin值.来看以下代码: <style type ...

  7. Unity3d纹理压缩格式表

  8. sqlite3编程使用简介

    sqlite3使用范围 SQLite不同于其他大部分的SQL数据库引擎,因为它的首要设计目标就是简单化: 1.易于管理 2.易于使用 3.易于嵌入其他大型程序 4.易于维护和配置  许多人喜欢SQLi ...

  9. JDBC getMetaData将结果集组装到List

    transient List query(Config config, Connection conn, String sql, Object paras[]) throws SQLException ...

  10. C#中IP地址转换为数值的方法

    任何语言都通用的方法转换 IP 地址 a.b.c.d ==> a***+b**+c*+d ===> *(c+*(b+*a)) +d 示例: ***+**+*+ ===> *( +*( ...