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

Return6.

思路

  1. 这道题在让求树中任意起始结束结点的最大路径,由于我们不知道起始和结束结点的位置,一个路径中必定有一个根节点,所以我们把树中每个结点对应的最大路径值记录在每个结点上。注意:这个节点值是从该节点遍历下去的一条最大路径的值,除了该节点,其子节点计算的值都只有一个子树。根据先计算子节点再计算根节点,我们选择后序遍历二叉树。比如

    5
       / \
      3 8
    / \ / \
    2 4 6 9
    /      \ \
    1      7 10

每个节点对应的值为

32
    / \
   7 27
  / \ / \
3 4 13 19
/        \ \
1          7 10

  1. 这里存储的每个节点的值和最后的值不是一个值,我们定义一个sum变量来存储我们要求的值,每遍历到一个节点时,我们需要将该节点的左右子节点存储的值相加,看看该节点对应的路径是否是比sum大,大的话就替换掉sum当前值
  2. 还要注意:树的节点的值可能是负数,所以要对每个节点左右子节点的返回值判断一下,若比0小,就赋值为0;初始化sum时不要赋值为0,要赋值为最小值0x80000000(int的最大值为0x7fffffff)

代码

/**

 * Definition for
binary tree

 * public class
TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode(int x) { val = x; }

 * }

 */

public class Solution {

int sum = 0x80000000; // 赋值为最小值

public int maxPathSum(TreeNode
root) {

if (root == null) {

return 0;

}

getSum(root);

return sum;

}

public int getSum(TreeNode
root) {

if (root == null) {

// 没有节点,返回0

return 0;

}

// 后序遍历

int leftNum =
getSum(root.left);

int rightNum =
getSum(root.right);

// 判断子节点的值是否小于0

if (leftNum < 0) {

leftNum = 0;

}

if (rightNum < 0) {

rightNum = 0;

}

// 计算当前路径值,和现有sum比较

int temp = leftNum +
rightNum + root.val;

if (temp > sum) {

sum = temp;

}

// 返回左右子节点中值比较大的一个

return (leftNum > rightNum
? leftNum : rightNum) + root.val;

}

}

时间: 2024-09-30 00:36:47

binary-tree-maximum-path-sum-LeetCode的相关文章

Binary Tree Maximum Path Sum leetcode java

题目: 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. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下: 1

Binary Tree Maximum Path Sum [leetcode] dp

a(i):以节点i作为终点的单边最大路径和 b(i):以节点i作为终点的双边边最大路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->right) }}; b(i) = max{ i->val, i->val + max{a(i->left), a(i->right) } , i->val + a(i->left) + a(i->right)}; 由于a(i), b(i)仅仅和a(i-

[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 左子树 右子树 和根节点形成最大路径 [代码] /******

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m

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][Tree][Binary Tree Maximum Path Sum]

找书中权值和最大的路径,至少包含一个点. 有点类似LCA(最近公共祖先),树的问题关键是如何划分子问题,然后递归求解. 想到了可以返回两种结果,一个是单独路径的最大和,一种是子树的最大和,然后在求解的过程中不断的更新答案. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val

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 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

【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 Return 6. 用递归确定每一个节点作为root时,从root出发的最长的路径 在每一次递归中计算maxPath 1 /** 2 * Def