124-Binary Tree Maximum Path Sum

标签: 算法 数据结构 leetcode
7人阅读 评论(0) 收藏 举报
分类:

Description:
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 must contain at least one node and does not need to go through the root.


For example:
Given the below binary tree,
这里写图片描述
Return 6.


问题描述:
给定二叉树,返回最大路径和
对于这个问题,序列被定义为通过父子连接由一个起始节点到终止节点的任意序列。路径至少有一个节点,并且不必经过根节点


问题分析:
通过后序遍历获取左右路径中最大的路径和(注意路径之和若小于0则置为0),然后与根节点组合形成最大路径和,若大于res,则更新res


解法

class Solution {
    int res = Integer.MIN_VALUE;

    public int maxPathSum(TreeNode root) {
        if(root == null)    return 0;

        DFS(root);

        return res;
    }
    public int DFS(TreeNode root){
        if(root == null)    return 0;

        int left = Math.max(0, DFS(root.left));
        int right = Math.max(0, DFS(root.right));

        res = Math.max(res, left + right + root.val);

        return root.val + Math.max(left, right);
    }
}
查看评论

【LeetCode】Binary Tree Maximum Path Sum 解题报告

【题目】 Given a binary tree, find the maximum path sum. The path may start and end at any node ...
  • ljiabin
  • ljiabin
  • 2015-01-22 16:15:05
  • 3408

Binary Tree Maximum Path Sum -- LeetCode

原题链接: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/  这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可...
  • linhuanmars
  • linhuanmars
  • 2014-04-05 02:19:00
  • 14529

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 seq...
  • fly_yr
  • fly_yr
  • 2015-12-27 16:20:25
  • 2011

LeetCode 124: Binary Tree Maximum Path Sum 树形动态规划

博客转载请注明地址:http://blog.csdn.net/sunliymonkey/article/details/48179427题目描述题目地址:https://leetcode.com/pr...
  • SunliyMonkey
  • SunliyMonkey
  • 2015-09-02 15:52:14
  • 1478

LeetCode Binary Tree Maximum Path Sum 解题报告

返回树中任意两点路径的最大值,只要两点间有路径可以到达就算。
  • worldwindjp
  • worldwindjp
  • 2014-02-06 22:43:45
  • 11031

Path Sum II -- LeetCode

原题链接: http://oj.leetcode.com/problems/path-sum-ii/  这道题是树的题目,跟Path Sum的要求很接近,都是寻找从根到叶子的路径。这道题目的要求是求...
  • linhuanmars
  • linhuanmars
  • 2014-04-14 23:59:49
  • 12634

[LintCode] Binary Tree Maximum Path Sum (二叉树中的最大路径和)

Binary Tree Maximum Path Sum
  • fanfan_U
  • fanfan_U
  • 2015-08-02 21:29:31
  • 1352

LeetCode | 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...
  • lanxu_yy
  • lanxu_yy
  • 2013-09-21 20:17:00
  • 3320

[leetcode-124]Binary Tree Maximum Path Sum(c++)

问题描述: Given a binary tree, find the maximum path sum.The path may start and end at any node in the ...
  • zdavb
  • zdavb
  • 2015-08-12 15:49:49
  • 225

[LeetCode 124] Binary Tree Maximum Path Sum

题目链接:binary-tree-maximum-path-sum /** * * Given a binary tree, find the maximum path sum. *...
  • ever223
  • ever223
  • 2015-04-14 23:30:31
  • 504
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 2万+
    积分: 6373
    排名: 4789
    博客专栏
    文章存档
    最新评论