题目

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,那么最长路应该就是..

F(left) + F(right) + val...当然如果left,或者right<0就不用加了的= =

从下往上递归遍历...

如果不这个不是root,那么就不能把left和right加起来了...因为只是一条路...

AC代码

 /**
* 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 maxVal = INT_MIN;
int maxPathSum(TreeNode* root) {
if (root == NULL)
return ; maxSum(root);
return maxVal;
} /*递归函数*/
int maxSum(TreeNode *root)
{
if (root == NULL)
return ; /*求以root为根的当前子树的最大路径和*/
int curVal = root->val;
int lmaxSum = maxSum(root->left), rmaxSum = maxSum(root->right);
if (lmaxSum > )
curVal += lmaxSum;
if (rmaxSum > )
curVal += rmaxSum; if (curVal > maxVal)
maxVal = curVal; /*返回以当前root为根的子树的最大路径和*/
return max(root->val, max(root->val + lmaxSum, root->val + rmaxSum));
}
};

LeetCode(124) Binary Tree Maximum Path Sum的更多相关文章

  1. (算法)Binary Tree Max Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

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

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

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

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

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

  6. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

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

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

  9. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

随机推荐

  1. Java:多态(向上转型)

    先来看一段代码: class BaseClass{          public int book = 6;          public void base(){         System. ...

  2. python学习心得第四章

     python 学习心得第四章 1.lambda表达式 1:什么是lambda表达式 为了简化简单函数的代码,选择使用lambda表达式 上面两个函数的表达式虽然不一样,但是本质是一样的,并且lamb ...

  3. PHP面向对象.__set(),__get(),__isset(),__unset()四个方法的

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...

  4. VC++ 在使用 CImage 的Draw 输入一个图像时,有时候会造成图像失真严重,解决的方法如下

    VC++  在使用 CImage 的Draw 输入一个图像时,有时候会造成图像失真严重,解决的方法如下 失真主要是由于变形造成的.只要设置一下变形的模式就可以了 ::SetStretchBltMode ...

  5. VC++ 对话框程序响应键盘消息的处理方法的说明(非常重要)

    基于MFC对话框的应用程序在响应按键消息和热键方面都力不从心,CDialog类的消息循环中去掉了TranslateAccelerator函数,因此不能响应热键:同时由于对话框上可能有很多控件,且默认情 ...

  6. 3D旋转相册(适合新手)

    <!DOCTYPE HTML> <html onselectstart="return false"> <head> <meta char ...

  7. LDAP与Samba

    默认的Samba服务器支持本地系统用户(smbpasswd添加后)访问Samba资源,不支持OpenLDAP服务器账号访问Samba共享资源 目的:配置完后,OpenLDAP每新增一个用户,就自动支持 ...

  8. php 快速排序

    快速排序是以其中一个数为比较标准,其他比较的数分块处理,应用递归按相同想法处理数据 比如:4 3 6 2 1 7 8 以4为比较对象 排序为 3 2 1 6 7 8 左边为 3 2 1 右边 为 6 ...

  9. linux内核升级图文攻略(转)

    一.Linux内核概览Linux是一个一体化内核(monolithic kernel)系统.设备驱动程序可以完全访问硬件.Linux内的设备驱动程序可以方便地以模块化(modularize)的形式设置 ...

  10. modelsim仿真vivado自动化脚本

    quit -sim set PATH1 C:/modeltech64_10.2c/xilinx144_lib set PATH2 C:/xilinx1/Vivado/2014.4/data/veril ...