int maxSum = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return maxSum;
}
public int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int left = Math.max(dfs(root.left), 0);
int right = Math.max(dfs(root.right), 0);
maxSum = Math.max(maxSum, left + right + root.val);
return Math.max(left, right) + root.val;
}
二叉树的最大路径和
最新推荐文章于 2024-07-25 13:54:27 发布