题目

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. 什么是JVM?

    什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的 ...

  2. [delphi]极域学生端解除键盘鼠标锁定退出全屏广播-强制窗口化-源代码

    v2.0  2015-07-11 更新了V2.0 版本 发布在吾爱破解论坛 欢迎下载使用 http://www.52pojie.cn/thread-382769-1-1.html ---------- ...

  3. 线程小demo

    下午就手写了两个demo,整理了一下. #!/sur/bin/env python # -*- coding:utf-8 -*- __author__ = 'ganzl' import threadi ...

  4. 谈谈springMVC和Strut2的理解

    关于struts2框架原理 执行流程 struts2框架的核心是一个过滤器,我们编写的action类都继承ActionSupport的接口(顶层是一个过滤器filter),用户发送请求,经过核心过滤器 ...

  5. 循序渐进Python3(十一) --2-- web之javascript

      JavaScrip                JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之后 ...

  6. Android Dex文件格式(二)

    第三块: 数据区         索引区中的最终数据偏移以及文件头中描述的map_off偏移都指向数据区, 还包括了即将要解析的class_def_item, 这个结构非常重要,下面就开始解析   c ...

  7. A. Writing Code 完全背包

    http://codeforces.com/contest/543/problem/A 一开始这题用了多重背包做,结果有后效性. 就是如果6,这样拆分成 1 + 2 + 3的,那么能产生3的就有两种情 ...

  8. jQuery与Ajax的应用——《锋利的jQuery》(第2版)读书笔记3

    第6章 jQuery与Ajax的应用 jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$ ...

  9. 安装运行okvis

    1. 安装依赖项 sudo apt-get install cmake   //cmake sudo apt-get install libgoogle-glog-dev  // glog是Googl ...

  10. Oracle 去除两边空格

    sql 去掉两头空格sql语法中没有直接去除两头空格的函数,但有ltrim()去除左空格rtrim()去除右空格.合起来用就是sql的trim()函数,即select ltrim(rtrim(UsrN ...