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 end at any node in the tree.

For example:
Given the below binary tree,

1
      / \
     2   3

SOLUTION 1:

计算树的最长path有2种情况:

1. 通过根的path.

(1)如果左子树从左树根到任何一个Node的path大于零,可以链到root上

(2)如果右子树从右树根到任何一个Node的path大于零,可以链到root上

2. 不通过根的path. 这个可以取左子树及右子树的path的最大值。

所以创建一个inner class:

记录2个值:

1. 本树的最大path。

2. 本树从根节点出发到任何一个节点的最大path.

注意,当root == null,以上2个值都要置为Integer_MIN_VALUE; 因为没有节点可取的时候,是不存在solution的。以免干扰递归的计算

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public class ReturnType {
12         int maxSingle;
13         int max;
14         ReturnType (int maxSingle, int max) {
15             this.max = max;
16             this.maxSingle = maxSingle;
17         }
18     }
19
20     public int maxPathSum(TreeNode root) {
21         return dfs(root).max;
22     }
23
24     public ReturnType dfs(TreeNode root) {
25         ReturnType ret = new ReturnType(Integer.MIN_VALUE, Integer.MIN_VALUE);
26         if (root == null) {
27             return ret;
28         }
29
30         ReturnType left = dfs(root.left);
31         ReturnType right = dfs(root.right);
32
33         int cross = root.val;
34
35         // if any of the path of left and right is below 0, don‘t add it.
36         cross += Math.max(0, left.maxSingle);
37         cross += Math.max(0, right.maxSingle);
38
39         // 注意,这里不可以把Math.max(left.maxSingle, right.maxSingle) 与root.val加起来,
40         // 会有可能越界!
41         int maxSingle = Math.max(left.maxSingle, right.maxSingle);
42
43         // may left.maxSingle and right.maxSingle are below 0
44         maxSingle = Math.max(maxSingle, 0);
45         maxSingle += root.val;
46
47         ret.maxSingle = maxSingle;
48         ret.max = Math.max(right.max, left.max);
49         ret.max = Math.max(ret.max, cross);
50
51         return ret;
52     }
53 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/MaxPathSum.java

时间: 2024-10-07 06:39:36

LeetCode: 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. [解析] 题意:在二叉树中找一条路径,使得该路径的和最大.该路径可以从二叉树任何结点开始,也可以到任何结点结束. 思路:递归求一条经过root的最大路径,这条路径可能是:

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