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 of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6.
求最大路径。
就是记录两个结果。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxPathSum(TreeNode root) {
if( root == null)
return 0;
long result[] = helper(root); return (int)Math.max(result[0], result[1]);
} public long[] helper(TreeNode node){
long[] result = new long[2];
result[0] = Integer.MIN_VALUE;
result[1] = Integer.MIN_VALUE;
if( node == null )
return result;
result[0] = node.val;
result[1] = node.val;
if( node.left == null && node.right == null)
return result; long[] num1 = helper(node.left);
long[] num2 = helper(node.right); result[0] = Math.max(Math.max(num1[0],num2[0])+node.val,node.val);
result[1] = Math.max(Math.max(Math.max(Math.max(Math.max(num1[1],num2[1]),num1[0]+num2[0]+node.val),num1[0]+node.val),
num2[0]+node.val),node.val); return result;
}
}
leetcode 124. Binary Tree Maximum Path Sum ----- java的更多相关文章
- 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 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
- 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 求二叉树的最大路径和
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 (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二叉树最大路径和
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
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
随机推荐
- [转载]Android View.onMeasure方法的理解
2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...
- 介绍几个java把网页报存为图片的框架
java在图像这一块非常弱.用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多 ...
- IOS多线程之NSOperation学习总结
NSOperation简介 1.NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 2.NSOperation和NSOperationQu ...
- linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程
1.设置任务调度命令crontab 任务调度是指系统在某个时间执行的特定的命令或程序.任务调度分为:1)系统工作:有些重要的工作必须周而复始的执行,如病毒扫描.2)个别用户工作:个别用户可能希望执行某 ...
- OBJECT ARX 添加标注样式
////获得当前图形的标注样式表 AcDbDimStyleTable* pDimStyleTbl; acdbHostApplicationServices()->workingDatabase( ...
- 北大poj-1001
Description Problems involving the computation of exact values of very large magnitude and precision ...
- (转)深入理解javascript的function
原文:http://www.cnblogs.com/sharpxiajun/archive/2011/09/16/2179323.html javascript笔记:深入理解javascript的fu ...
- AppCan相关网站
AppCan文档中心: http://doc.appcan.cn/#!/guide/handbook AppCan官网: http://www.appcan.cn/index.html
- H5实现俄罗斯方块(二)
对应的js 1.封装操作dom的js: (function (document) { //游戏的实例 var gameInst; /**封装一个返回原型的DOM对象 */ function DomOb ...
- swift初识
介绍: Swift是苹果2014年推出的全新编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程不仅保留了Objc很多语言特性,他也借鉴了多种现代化语言的特点,在其 ...