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.
For example:
Given the below binary tree,
1
/ \
2 3
Return 6
.
解题思路:
DFS暴力枚举,注意,如果采用static 全局变量的话,在IDE里面是可以通过,但在OJ上无法测试通过,因此需要建立一个类来储存结果,JAVA实现如下:
public class Solution {
static public int maxPathSum(TreeNode root) {
Result result=new Result(Integer.MIN_VALUE);
dfs(root,result);
return result.val;
}
static int dfs(TreeNode root,Result result) {
if (root == null)
return 0;
int left_sum = Math.max(0, dfs(root.left,result));
int right_sum = Math.max(0, dfs(root.right,result));
result.val = Math.max(result.val, left_sum + right_sum + root.val);
return Math.max(left_sum, right_sum) + root.val;
}
}
class Result{
int val;
Result(){
this.val=Integer.MIN_VALUE;
}
Result(int val){
this.val=val;
}
}
Java for 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 二叉树中的最大路径和 (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 ...
- [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 ...
随机推荐
- iOS网络交互数据格式解析之json
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式.从ios5开 始,apple提供了对json的原生支持,但为了兼容以前的ios版本,我们仍然需要使用第三方库来解析常用 ...
- mac 安装 gensim包出错
安装时需要卸载scipy,结果显示 permission之类 加sudo也不行, 必须 得先disable 掉mac的SIP ,方法是重启系统 ,按住command+r ,进行recovers模式,然 ...
- VS2010 MFC中 创建文件夹及文件判空的方法
1. MFC中 创建文件夹的方法如下: CString strFolderPath = "./Output"; //判断路径是否存在 if(!PathIsDirectory(str ...
- MFC中 日期字符串的转换
一.将字符串2011-08-1800:00:00转换为字符串2011-8-18,通过以下的函数 CString DataDeleteZero(CString DATA) { CStringstrmon ...
- 转:Kafka、RabbitMQ、RocketMQ消息中间件的对比 —— 消息发送性能 (阿里中间件团队博客)
from: http://jm.taobao.org/2016/04/01/kafka-vs-rabbitmq-vs-rocketmq-message-send-performance/ 引言 分布式 ...
- Esper epl语句实验
基础代码见下,下文列举的实验都是在此程序基础上改动. all,snapshot,first String epl = "select * from appTable.win:time(5 s ...
- centos DHCP
yum install dhcp cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example > /etc/dhcp/dhcpd.conf vim /etc ...
- vuex Payload 荷载
1.payload payload:有效载荷,即记录有效信息的部分. 通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如这一批数据量的大小 ...
- [Android]egit取消文件版本号控制
开发项目,多人合作开发变得越来越重要了,在此同一时候,使用git作为协同工具也是越来越多.在此.介绍一下egit取消文件版本号控制的方法. (egit即为eclipse中的git插件) 1.打开Nav ...
- 国内云引擎平台概览——新浪SAE,阿里ACE,百度BCE
新浪SAE 平时大家的測试server都是执行在自己的PC上面,用Tomcat或者IIS搭建的本机server. 事实上新浪云平台SinaAppEngine也是挺好用的. 今天总结一下我使用过程中的一 ...