LeetCode Top 100 Liked Questions 124. Binary Tree Maximum Path Sum (Java版; Hard)
题目描述
Given a non-empty 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.
Example 1:
Input: [1,2,3]
1
/ \
2 3
Output: 6
Example 2:
Input: [-10,9,20,null,null,15,7]
-10
/ \
9 20
/ \
15 7
Output: 42
classSolution{privateint max = Integer.MIN_VALUE;publicintmaxPathSum(TreeNode root){core(root);return max;}privateintcore(TreeNode root){if(root==null){return0;}int L =core(root.left);int R =core(root.right);
max = Math.max(max, root.val + Math.max(0, L)+ Math.max(0, R));return root.val + Math.max(0, Math.max(L, R));}}
LeetCode Top 100 Liked Questions 124. Binary Tree Maximum Path Sum (Java版; Hard)
welcome to my blogLeetCode Top 100 Liked Questions 124. Binary Tree Maximum Path Sum (Java版; Hard)题目描述Given a non-empty binary tree, find the maximum path sum.For this problem, a path is defined ...