# Hard-题目32：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 of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root.

For example:
Given the below binary tree,

   1
/ \
2   3


Return 6.

public class Solution {
public int max = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
rec(root);
return max;
}

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

int leftSubtreeMaxSum = rec(root.left);     // 左子树的最大和
int rightSubtreeMaxSum = rec(root.right);       // 右子树的最大和
int arch = leftSubtreeMaxSum + root.val + rightSubtreeMaxSum;
int maxPathAcrossRootToParent = Math.max(root.val, Math.max(leftSubtreeMaxSum, rightSubtreeMaxSum)+root.val);
max = Math.max(max, Math.max(arch, maxPathAcrossRootToParent));

return maxPathAcrossRootToParent;
}
}

2ms,23.37%,2ms,76.33%
cmershen的碎碎念：

• 本文已收录于以下专栏：

## leetcode-124:Binary Tree Maximum Path Sum(Java)

Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may st...

## leetcode解题之124. Binary Tree Maximum Path Sum Java版 (二叉树的最大路径和）

124. Binary Tree Maximum Path Sum ，leetcode解题之124. Binary Tree Maximum Path Sum Java版 (二叉树的最大路径和）...

## [leetcode]124. Binary Tree Maximum Path Sum@Java解题报告

https://leetcode.com/problems/binary-tree-maximum-path-sum/discuss/ Given a binary tree, ...

## Leetcode #124 Binary Tree Maximum Path Sum

Binary Tree Maximum Path Sum题目描述 给出一颗二叉树，每个树节点有一个整数权值。 现在要求从树上找出一条路径，使得权值之和最大，问这个最大权值之和是多少。算法分析 一...

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

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

## LeetCode（124） Binary Tree Maximum Path Sum

• fly_yr
• 2015年12月27日 16:20
• 1784

## 124. Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen...

举报原因： 您举报文章：Hard-题目32：124. Binary Tree Maximum Path Sum 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)