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 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.
给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和)
要找到二叉树种任意一点到任意一点相加的最大值,分三种情况,
一种是从跟节点左侧的某个点走到跟节点右侧某个点,
第二种是从跟节点左侧的某个点走到跟节点左侧的某个点,
第三种是从跟节点右侧的某个点到跟节点右侧的某个点。
current只是当前值和左侧最大和右侧最大的三者的比较,culculateSum这个函数的功能是递归求出某个点的左侧路径和右侧路径和自身值的三者的最大值,
max[0]记录某个点左侧路径、右侧路径、自身、左侧加右侧加自身 四者之间的最大值。
/**
* 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) {
int max[] = new int [1];
max[0] = Integer.MIN_VALUE;
culculateSum(root, max);
return max[0];
} public int culculateSum(TreeNode root, int[] max) {
if (root == null) {
return 0;
} int left = culculateSum(root.left, max);
int right = culculateSum(root.right, max); int current = Math.max(root.val, Math.max(root.val + left, root.val + right));
max[0] = Math.max(max[0], Math.max(current, left + root.val + right));
return current;
}
}
leetcode 124. Binary Tree Maximum Path Sum的更多相关文章
- 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 (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 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
- 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 ...
随机推荐
- 面向服务架构(SOA)和企业服务总线(ESB)
http://www.cnblogs.com/shanyou/archive/2008/04/19/1161452.html 学习和研究在企业中实施面向服务架构(SOA),简单回顾SOA和ESB,重点 ...
- Interface/接口
1. 类和结构能够实现接口 2. 接口声明包含如下四种类型:属性.方法.事件和索引:这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号 3. 继承接口的类或结构必须实现接口中的所有成 ...
- asp.net(C#)页面事件顺序
asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html 1 using System.Data; ...
- python递归理解图
递归:下一级只能return给自己的上一级. import re val="9-2*5/3+7/3*99/4*2998+10*568/14" val="9-2*5/3+7 ...
- MongoDB学习笔记(索引)(转)
一.索引基础: MongoDB的索引几乎与传统的关系型数据库一模一样,这其中也包括一些基本的优化技巧.下面是创建索引的命令: > db.test.ensureIndex({" ...
- spring InitializingBean接口
最近工作需要得到sping中的每个事物需要执行的sql,称机会简单研究了一下spring的事务,项目中管理事务比较简单,用TransactionTemplate,就直接以TransactionTemp ...
- HTML5+ 学习笔记3 storage.增删改查
//插入N条数据 function setItemFun( id ) { //循环插入100调数据 var dataNum = new Number(id); for ( var i=0; i< ...
- [译]ngclass expressions in angularjs
原文: http://blog.xebia.com/2014/01/31/ngclass-expressions-in-angularjs/ ngClass 指令允许你通过databinding一个表 ...
- AngularJS API之extend扩展对象
angular.extend(dst,src),在我实验的1.2.16版本上是支持深拷贝的.但是最新的API显示,这个方法是不支持深拷贝的. 另外,第二个参数src支持多个对象. 第一种使用方式 va ...
- EF方便的添加一条信息...
//刚开始通过EF添加数据都是这样的...↓ var db = new DBEntities() T_User t_userinfo = new T_User() { Type = "typ ...