树——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
Return6.
思路:
用递归方法从叶节点开始,将所求最大路径和maxValue设为全局变量,并赋初始值。
假设递归到节点n,首先计算左子树的最大路径和left,并与0进行比较,若left<0,则left=0。同理求出右子树最大路径和right。
将maxValue与left+right+root.val比较,把较大值赋于maxValue。
递归函数的函数值为节点n.val与左右子树中较大值的和。
代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxValue = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) {
if(root==null)
return 0;
max(root);
return maxValue;
}
public int max(TreeNode root){
if(root==null)
return 0;
int left = Math.max(0, max(root.left));
int right = Math.max(0, max(root.right));
maxValue = Math.max(maxValue, left+right+root.val);
return Math.max(left, right)+root.val;
}
}
树——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 Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
- 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 ...
- [LeetCode] 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] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 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]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 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 ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 【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 ...
随机推荐
- bootstrap-multiselect 设置单选无效(设置单选依然是复选框)
bootstrap-multiselect 的使用介绍:https://www.cnblogs.com/landeanfen/p/5013452.html bootstrap-multiselect ...
- Spring MVC Captcha 验证码
本文专为不想一遍遍手写验证码代码的”懒猿“而生,只需要添加部分配置文件,拷贝一个控制层代码,就能在页面中方便使用.话不多说,进入主题: 一. Spring MVC 环境实现方式: 1.1 spring ...
- SpringBoot项目属性配置-第二章
SpringBoot入门 1. 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了 ...
- Java 实现 对象和转字符串之间的互转 (json格式)
添加依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId> ...
- p2619 [国家集训队2]Tree I [wqs二分学习]
分析 https://www.cnblogs.com/CreeperLKF/p/9045491.html 反正这个博客看起来很nb就对了 但是不知道他在说啥 实际上wqs二分就是原来的值dp[x]表示 ...
- P1058立体图
一道大模拟 思路: 首先是打表找规律时间 仔细思考(暴力手算)后推出这么一个数组: //宽的增加量 ]={,,,};//1竖着摞,2横着摞,3前后摞 //长的增加量 ]={,,,};//1竖着摞,2横 ...
- Gradel 多渠道打包 代码混淆
http://blog.csdn.net/ttccaaa/article/details/47687241 http://www.bubuko.com/infodetail-987756.html h ...
- c# Thread——1.为什么Abort中断线程是不可靠的
Thread.Abort 方法在c#中用作强制中断线程的执行,大多用于线程内部满足某个特定条件而自己调用关闭自身,比如下面的代码在i自增到3的时候就会停止打印. class Program { sta ...
- 分布式ID生成器 snowflake(雪花)算法
在springboot的启动类中引入 @Bean public IdWorker idWorkker(){ return new IdWorker(1, 1); } 在代码中调用 @Autowired ...
- python爬虫——爬取淘票票正在热映电影
今天正好学习了一下python的爬虫,觉得收获蛮大的,所以写一篇博客帮助想学习爬虫的伙伴们. 这里我就以一个简单地爬取淘票票正在热映电影为例,介绍一下一个爬虫的完整流程. 首先,话不多说,上干货——源 ...