题目:

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. PHP登陆页面完整代码

      /* 包括的文件 */ /* login.php */ <?phprequire('./mysql.php');$username=$_REQUEST['username'];$passwd ...

  2. Jmeter - Linux 下面执行jmeter-server的时候出现:An error occurred: Cannot start. localhost is a loopback address.错误

    Jmeter - Linux 下面执行jmeter-server的时候出现:An error occurred: Cannot start. localhost is a loopback addre ...

  3. JavaScript深入理解对象方法——Object.entries()

    Object.entries()方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-in 循环也枚举原型链中的属性) 示例 ...

  4. java理解抽象类 2.19

    // Telphone.java public abstract class Telphone{ public abstract void call(); public abstract void m ...

  5. eshop4-tomcat 安装

    1. 下载tomcat 7 2. 解压缩 注意:是否使用sudo 权限执行请根据具体环境来决定 3. sudo vim /etc/profile 在最下方增加 export CATALINA_HOME ...

  6. Educational Codeforces Round 64 选做

    感觉这场比赛题目质量挺高(A 全场最佳),难度也不小.虽然 unr 后就懒得打了. A. Inscribed Figures 题意 给你若干个图形,每个图形为三角形.圆形或正方形,第 \(i\) 个图 ...

  7. 解析underscore中的debounce

    先奉上源码 取自Underscore.js 1.9.1的debounce _.debounce = function(func, wait, immediate) { var timeout, res ...

  8. Spring boot application.properties和 application.yml 初学者的学习

    来自于java尚硅谷教程 简单的说这两个配置文件更改配置都可以更改默认设置的值比如服务器端口号之类的,只需再文件中设置即可, properties可能是出现的比较早了,如果你不调你的默认编码,中文可能 ...

  9. redmine处理规范

         开发: 1.       研发人员负责更新到的状态共有三个:  “进行中”. ”已解决”. ”需要反馈”. 2.       在开始修复bug的时候,把状态更新为”进行中”,把title更新 ...

  10. docker 后台运行和进入后台运行的容器

    先创建并进入一个新的被命名为newos的新容器    docker run -it --name newos docker.io/centos #创建并指定端口号映射 docker run -d -p ...