题目:

Given a non-empty 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 must contain at least one node and does not need to go through the root.

Example 1:

Input: [1,2,3]

       1
/ \
2 3 Output: 6

Example 2:

Input: [-10,9,20,null,null,15,7]

   -10
   / \
  9  20
    /  \
   15   7 Output: 42

分析:

给定一颗二叉树,求其最大路径,路径就是从任意节点出发,到达任意节点的序列,注意路径至少要有一个节点。

这道题和下面两道题做法相同,都是求二叉树的路径问题,可以先回顾下面两个问题。

LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

那么这道题还是从根节点递归求解,当前结点的最大路径,是当前节点的值加上左右孩子的最大路径,同时和全局的最大值进行比较,更新最大值,而作为返回值时,需要在左右孩子中选取最大值加上当前结点的值同时和0比较,选取最大值,因为节点存在负数,那么路径为负数显然是不对的,所以对于值为负数的子树我们向上层返回0即可。

程序:

C++

/**
* 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 maxPathSum(TreeNode* root) {
if(root == nullptr)
return ;
int res = INT_MIN;
maxPathSum(root, res);
return res;
}
private:
int maxPathSum(TreeNode* root, int& res){
if(root == nullptr)
return ;
int l = maxPathSum(root->left, res);
int r = maxPathSum(root->right, res);
int sum = l + r + root->val;
res = max(sum, res);
return max(max(l, r) + root->val, );
}
};

Java

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxPathSum(TreeNode root) {
if(root == null)
return 0;
res = Integer.MIN_VALUE;
maxPath(root);
return res;
}
private int res;
private int maxPath(TreeNode root) {
if(root == null)
return 0;
int l = maxPath(root.left);
int r = maxPath(root.right);
int sum = l + r + root.val;
res = Math.max(sum, res);
return Math.max(Math.max(l, r) + root.val, 0);
}
}

LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)的更多相关文章

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

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

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

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

  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 (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

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

  8. Java for LeetCode 124 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. ...

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

随机推荐

  1. P2312 解方程(随机化)

    P2312 解方程 随机化的通俗解释:当无法得出100%正确的答案时,考虑随机化一波,于是这份代码很大可能会对(几乎不可能出错). 比如这题:把系数都模一个大质数(也可以随机一个质数),然后O(m)跑 ...

  2. HiBench成长笔记——(4) HiBench测试Spark SQL

    很多内容之前的博客已经提过,这里不再赘述,详细内容参照本系列前面的博客:https://www.cnblogs.com/ratels/p/10970905.html 和 https://www.cnb ...

  3. 064、Java中递归调用

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  4. 认识json,详解JsonConfig

    说到json 初学者很迷茫,不知json怎么为何物,以及怎么用.我简单说下我的了解 既然用了json 我们就要知其然也知其所以然.下面有几个疑问 1.为什么要用json?也就是json 的优势 2.我 ...

  5. eclipse、idea中自动生成元模型JPA元模型对象

    一.eclipse 1.首先准备好两个jar包hibernate-jpa-2.0-api-1.0.1.Final和hibernate-jpamodelgen-4.3.5.Final 2.选中项目右击 ...

  6. C#最小化到托盘+双击托盘恢复+禁止运行多个该程序

    托盘程序的制作: 1.添加notifyIcon控件,并添加Icon,否则托盘没有图标(托盘右键菜单也可直接在属性里添加):2.响应Form的Resize或SizeChanged消息: // Hide ...

  7. url中?的作用

    http://123.206.87.240:8002/get/?what=flag? 分隔实际的URL和参数 ,用于动态页面的交互和传参

  8. 2.8 学习总结 之 JQ初识

    一.说在前面 昨天 学习了kotlin的相关知识 今天 学习JQ Jquery它是javascript的一个轻量级框架,对javascript进行封装,它提供了很多方便的选择器.供你快速定位到需要操作 ...

  9. fiddler 限速方法

    1.使用的软件下载地址: \\192.168.100.2\共享软件\开发常用\flash_team\工作软件\fiddler2setup.exe 2.注意事项 测试是,在ie浏览器环境下测试 3.软件 ...

  10. 破解centos7 密码

    1.在CentOS7的启动选项,按“e”选择编辑启动选项2.进入下图画面,点下箭头直到看到“linux162174542514”,按end键跳到行尾3.在行尾加上“rd.break”,并敲击键盘“ct ...