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

For example:

Given the below binary tree,

       1
      /      2   3

Return 6.

【解析】

题意:在二叉树中找一条路径,使得该路径的和最大。该路径可以从二叉树任何结点开始,也可以到任何结点结束。

思路:递归求一条经过root的最大路径,这条路径可能是:

1) 左边某条路径 + root + 右边某条路径

2) 左边某条路径 + root

3) root + 右边某条路径

4) root

【Java代码】

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private int max = Integer.MIN_VALUE;

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

        maxSum(root);

        return max;
    }

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

        int leftVal = maxSum(root.left);    //递归求左支路的最大路径和
        int rightVal = maxSum(root.right);  //递归求右支路的最大路径和

        //如果当前局部解(root或left+root或root+right或left+root+right)是最有解,更新最终结果
        int curMax = root.val;
        if (leftVal > 0) {
            curMax += leftVal;
        }
        if (rightVal > 0) {
            curMax += rightVal;
        }
        if (curMax > max) {
            max = curMax;
        }

        //返回从叶子结点到root的最大路径和(root或left+root或root+right)
        return Math.max(root.val, Math.max(root.val + leftVal, root.val + rightVal));
    }
}

参考:http://blog.csdn.net/worldwindjp/article/details/18953987

时间: 2024-12-19 08:48:35

【LeetCode】Binary Tree Maximum Path Sum 解题报告的相关文章

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven 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 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] 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. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

[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 tree. For example: Given the below binary tree, 1 / 2 3 Return 6. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNo

[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 tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

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 3Return 6. 解题思路,递归 a b      c curmax = max (a+b, a, a+c) //计算当前节点单边最大值, 如果a+b 最大,那就是说,把左子树包含进来,有利可图 如果a

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 tree. For example: Given the below binary tree, 1 / 2 3 Return 6. 原题链接:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题目:给定一二叉树,求出最大路径和. 分析:

[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 tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 题目意思很简单,就是给定一棵二叉树,求最大路径和.path 可以从任意 node 开始,到任意 node 结束. 这道题在 LeetCode 上的通过率只有 20% 多一点,并被标记

[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 tree. For example:Given the below binary tree, 1 / 2 3 Return6. 思路:题目中说明起始节点可以是任意节点,所以,最大的路径和不一样要经过root,可以是左子树中某一条,或者是右子树中某一条,当然也可能是经过树的根节点root的.递归式是应该是这三者中

[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 tree. For example:Given the below binary tree, 1 / 2 3 Return 6. 这道求二叉树的最大路径和是一道蛮有难度的题,难就难在起始位置和结束位置可以为任意位置,我当然是又不会了,于是上网看看大神们的解法,看了很多人的都没太看明白,最后发现了网友Yu's C