Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

树结构显然用递归来解,解题关键:

1、对于每一层递归,只有包含此层树根节点的值才可以返回到上层。否则路径将不连续。

2、返回的值最多为根节点加上左右子树中的一个返回值,而不能加上两个返回值。否则路径将分叉。

在这两个前提下有个需要注意的问题,最上层返回的值并不一定是满足要求的最大值,

因为最大值对应的路径不一定包含root的值,可能存在于某个子树上。

因此解决方案为设置全局变量maxSum,在递归过程中不断更新最大值。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxSum;
Solution()
{
maxSum = INT_MIN;
}
int maxPathSum(TreeNode* root)
{
Helper(root);
return maxSum;
}
int Helper(TreeNode *root) {
if(!root)
return INT_MIN;
else
{
int left = Helper(root->left);
int right = Helper(root->right);
if(root->val >= )
{//allways include root
if(left >= && right >= )
maxSum = max(maxSum, root->val+left+right);
else if(left >= && right < )
maxSum = max(maxSum, root->val+left);
else if(left < && right >= )
maxSum = max(maxSum, root->val+right);
else
maxSum = max(maxSum, root->val);
}
else
{
if(left >= && right >= )
maxSum = max(maxSum, max(root->val+left+right, max(left, right)));
else if(left >= && right < )
maxSum = max(maxSum, left);
else if(left < && right >= )
maxSum = max(maxSum, right);
else
maxSum = max(maxSum, max(root->val, max(left, right)));
}
//return only one path, do not add left and right at the same time
return max(root->val+max(, left), root->val+max(, right));
}
}
};

【LeetCode】124. Binary Tree Maximum Path Sum的更多相关文章

  1. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  2. Leetcode solution 124: Binary Tree Maximum Path Sum

    Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path ...

  3. 【Lintcode】094.Binary Tree Maximum Path Sum

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

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

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

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

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

  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. LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)

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

随机推荐

  1. 关于Themleaf学习总结

    此篇记录学习Themleaf测试的相关用例: study01 Thymeleaf 的HelloWorld级别的例子 简单介绍Thymeleaf的工作流程 study02 使用spring.thymel ...

  2. MVC 部署在IIS7 出现的 404 错误

    如果你不幸在 windows server 2008 R2 的 IIS7 中部署 MVC 站点的话,如果你输入:http://yourdomain/Organization/Index ,那么你很有可 ...

  3. 使用spring中的@Transactional注解时,可能需要注意的地方

    前情提要 在编写业务层方法时,会遇到很多需要事务提交的操作,spring框架为我们提供很方便的做法,就是在需要事务提交的方法上添加@Transactional注解,比起我们自己开启事务.提交以及控制回 ...

  4. Python for everyone chapter 1

    Chapter 1 10 试题 1.  When Python is running in the interactive mode and displaying the chevron prompt ...

  5. typedef的用法和相关问题

    用了C和C++这么久,今天才仔细研究了下typedef的用法,真的是惭愧啊,不过基础都是不断巩固的啊. typedef 在计算机编程语言中用来为复杂的声明定义简单的别名,与宏定义有些差异.它本身是一种 ...

  6. 第十六章 springboot + OKhttp + String.format

    模拟浏览器向服务器发送请求四种方式: jdk原生的Http包下的一些类 httpclient(比较原始,不怎么用了):第一章 HttpClient的使用 Okhttp(好用,推荐) retrofit( ...

  7. ubuntu 12.04 安装无线网卡驱动

    安装ubuntu 12.04后,无线网卡不可用,采用以下方式解决: 1.在终端中运行如下命令,重新安装b43相关的全部驱动和firmware: sudo apt-get install bcmwl-k ...

  8. mysql必知必会(一、数据库基础知识)

    基础概念 1.数据库(database):是一个以某种有组织的方式存储的数据集合.(保存有组织的数据的容器) 2.表(table):是一种结构化的文件,可用来存储某种特定类型的数据.(表名在同个数据库 ...

  9. 使用C#创建及调用WCF完整实例 (Windows服务宿主)

    关于WCF的概念.原理.优缺点等,在这里就不多说了,网上很多,可以自行搜索,比我解释的要专业的多. 这里直接说使用Windows 服务(Windows Service)作为宿主如何实现,其它方式不在此 ...

  10. Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架

    一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...