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的更多相关文章

  1. 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 ...

  2. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. 每天学一点Python(2)

    9月16日(python扩展的安装和使用) 接着上一篇继续.按照之前计划,先分析导出的数据,再做进一步统计. 导出的数据是html类型的,想到的处理方法有: 1.直接readlines然后一行一行找我 ...

  2. hdu1420(C++)

    数论中模的运算: a*b%n=(a%n)*(b%n)%c; (a+b)%n=(a%n+b%n)%n; 幂的模:A^n%c=r    于是A^(n+1)%c=A*r%c; #include<ios ...

  3. foreach_break 面试记录

    版权所有@foreach_break] [博客地址 http://www.cnblogs.com/foreach-break] 可以转载,但必须注明出处并保持博客超链接 背景 自从2013年离开北京后 ...

  4. Hadoop本地调试

    windows上先调试该程序,然后再转到linux下. 程序运行的过程中, 报 Failed to locate the winutils binary in the hadoop binary pa ...

  5. Android的Framework分析---5 ActivityManager分析

    Android系统开发交流群:484966421 OSHome. 微信公众号:oshome2015 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font ...

  6. 应用设置Setting的实现

    有非常多应用都在iOS设置中有相关的设置.例如以下图:     通过这个设置能够方便的相应用的一些主要的设置进行更改. 要完整的实现这个设置功能,有下面几方面问题须要解决: 1)设置的编写(实现设置的 ...

  7. openssl之BIO系列之22---Cipher类型的BIO

    Cipher类型BIO ---依据openssl doc\crypto\bio_f_cipher.pod翻译和自己的理解写成 (作者:DragonKing, Mail: wzhah@263.net , ...

  8. 从头開始写项目Makefile(五):嵌套运行

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] 在大一些的项目里面,全部源码不会仅仅放在同一个文件夹,一般各个功 ...

  9. 在Ubuntu 14.04 64bit中永久添加DNS的方法

    DNS信息是由/etc/resolv.conf提供的,它是每次开机时,由/sbin/resolvconf生成的/etc/resolv.conf是/run/resolvconf/resolv.conf的 ...

  10. iOS常用的加密方式

    MD5 iOS代码加密 创建MD5类,代码如下 #import <Foundation/Foundation.h> @interface CJMD5 : NSObject +(NSStri ...