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 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)的更多相关文章
- [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 ...
- 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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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. ...
- 【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 ...
随机推荐
- 最简单的mybatis增删改查样例
最简单的mybatis增删改查样例 Book.java package com.bookstore.app; import java.io.Serializable; public class Boo ...
- java string常用的占位符形式
自己在这里总结了三种占位符形式:看下面代码即可 String stringFormat = "lexical error at position %s, encountered % ...
- 小程序列表循环出来的list是不同接口赋的值
需求:首页有三个列表,样式形式都是一样的,可以循环展示,但是循环的内容list部分是来自于不同的三个接口. data: { indexList:[{ name: "中考体能突击营" ...
- Ubuntu系统配置Zabbix前端及中文乱码解决方案
Ubuntu系统配置Zabbix前端及中文乱码解决方案 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装zabbix 博主推荐阅读: https://www.cnblogs ...
- Windows编程常用api
转载网络 黑客常用WIN API函数整理 一.进程 创建进程: CreateProcess (,,,,,,,&si,&pi); WinExec("notepad", ...
- Product of Polynomials
题意:多项式相乘,合并同类项后输出每一项的系数. 题目链接:https://www.patest.cn/contests/pat-a-practise/1009 分析:注意合并后系数为0,这一项就不存 ...
- 013、Java中int类型转换byte
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 001.Delphi插件之QPlugins,一个最简单的插件
安装QPlugins里面的Demo,复制粘贴着写了一个最简单的插件,看看好不好用 EXE代码如下: unit Main_Frm; interface uses Winapi.Windows, Wina ...
- leetcode303 Range Sum Query - Immutable
""" Given an integer array nums, find the sum of the elements between indices i and j ...
- ArcGIS二次开发的几种方式
1.ArcEngine开发 二次开发的常用方式,开发提供接口齐全,功能强大,比较成熟.但是,开发的软件使用需要指定版本的运行环境才能运行. 2.Addin开发 二次开发与ArcMap嵌入,开发方便,可 ...