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.


对于树,我们可以找到其左右子树中终止于根节点的最大路径值,称为最大半路径值,如果都是正数,则可以把它们以及根节点值相加,如果其中有负数,则舍弃那一边的值(即置为零)。如此可以找到包含根节点的最大路径值。然后选择左右子树的最大半路径值中大的那个(负数则置为0)加上根节点的值作为新的最大半路径值传给父节点。

于是我们可以递归地考察每个子树,不断更新一个全局变量max。

基本情况为:子树为null,此时最大路径值应为0.

代码如下:

     int max;
public int maxPathSum(TreeNode root) {
max = root==null?0:root.val;
findMax(root);
return max;
}
public int findMax(TreeNode root) {
if(root==null)
return 0;
int left = Math.max(findMax(root.left),0);
int right = Math.max(findMax(root.right),0);
max = Math.max(max, left+right+root.val);
return Math.max(left, right) + root.val;
}

[Leetcode][JAVA] Binary Tree Maximum Path Sum的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. sql(转自http://www.imooc.com/article/2325)

    http://www.imooc.com/article/2325

  2. 【练习】移动数据----infile *

    要求: ①指定bad文件: ②挂在之前将目标表delete: ③导入的的数据在控制文件中. 1.创建目录对象: :: SYS@ORA11GR2>create or replace directo ...

  3. zk抢主

    package com.autonavi.tinfo.t1.traffic.pub.openlr.util; import java.util.Collections;import java.util ...

  4. c++操作符重载

    一.类型转换操作符(type conversion operator)[1] 参考: [1]. C++类型转换操作符(type conversion operator): http://www.cpp ...

  5. nodejs Express 4.x req.body req.query req.params 三种获取参数的方法

    第一种情况:http://localhost:3000/1,我们可以用req.params.(应该是跟路由有关,待) 第二种情况:http://localhost:3000/?id=1,用req.qu ...

  6. linux+php+apache+mysql(mariadb)故障排除

    wordpress 网页文件打不开(client denied by server).白屏(http 500)问题排除顺序 1.查看apache错误日志查照问题报告找到问题 “client denie ...

  7. Visual Studio 2013 编译CEF步骤

    If you'd like to build the Chromium Embedded Framework (a wrapper for Chromium, for creating browser ...

  8. eclipse 使用

  9. 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式

    编解码学习笔记(一):基本概念 媒体业务是网络的主要业务之间.尤其移动互联网业务的兴起,在运营商和应用开发商中,媒体业务份量极重,其中媒体的编解码服务涉及需求分析.应用开发.释放 license收费等 ...

  10. 说一说inline-block的奇葩之处

    今天本来想聊一下margin和padding,但是当我给div加了一个display:inline-block之后,发现一个问题: .box_demo{border: 1px solid #333;w ...