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、返回的值最多为根节点加上左右子树中的一个返回值,而不能加上两个返回值。否则路径将分叉。

在这两个前提下有个需要注意的问题,最上层返回的值并不一定是满足要求的最大值,

因为最大值对应的路径不一定包含root的值,可能存在于某个子树上。

因此解决方案为设置全局变量maxSum,在递归过程中不断更新最大值。

/*** Definition for binary tree* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxSum;Solution(){maxSum = INT_MIN;}int maxPathSum(TreeNode* root){Helper(root);return maxSum;}int Helper(TreeNode *root) {if(!root)return INT_MIN;else{int left = Helper(root->left);int right = Helper(root->right);if(root->val >= 0){//allways include rootif(left >= 0 && right >= 0)maxSum = max(maxSum, root->val+left+right);else if(left >= 0 && right < 0)maxSum = max(maxSum, root->val+left);else if(left < 0 && right >= 0)maxSum = max(maxSum, root->val+right);elsemaxSum = max(maxSum, root->val);}else{if(left >= 0 && right >= 0)maxSum = max(maxSum, max(root->val+left+right, max(left, right)));else if(left >= 0 && right < 0)maxSum = max(maxSum, left);else if(left < 0 && right >= 0)maxSum = max(maxSum, right);elsemaxSum = max(maxSum, max(root->val, max(left, right)));}//return only one path, do not add left and right at the same timereturn max(root->val+max(0, left), root->val+max(0, right));}}
};

转载于:https://www.cnblogs.com/ganganloveu/p/4126953.html

【LeetCode】124. Binary Tree Maximum Path Sum相关推荐

  1. leetcode @python 124. Binary Tree Maximum Path Sum

    题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...

  2. 【重点】LeetCode 124. Binary Tree Maximum Path Sum

    LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...

  3. 124 Binary Tree Maximum Path Sum

    题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...

  4. LeetCode 124. Binary Tree Maximum Path Sum

    原题 求二叉树的最大路径和 Given a binary tree, find the maximum path sum. For this problem, a path is defined as ...

  5. 124. Binary Tree Maximum Path Sum

    题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...

  6. 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和

    Title 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 ...

  7. Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum 题目链接:https://leetcode.com/problems... dfs对每个node,查一下包含这个node的最大路径值. /** ...

  8. 【Binary Tree Maximum Path Sum】cpp

    题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...

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

最新文章

  1. 对Linux系统中的时钟和时间的探讨
  2. tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm.的一种解决方法
  3. 六、springboot(三)配置双数据源
  4. java 传递bean_如何将bean作为参数传递给JSP标记?
  5. java文本域 图片_java swing中给面板或者文本域设置背景图片的方法!
  6. 开启ssh不能用root登入的限制
  7. DeepFake技术--Deepfakes教程及各个换脸软件下载
  8. 电源大师课笔记 1.1
  9. LCD12864图形点阵液晶显示模块中文资料介绍
  10. 应用密码学(张仕斌)第四章
  11. unity3d手游破解(一)
  12. 车联网信息服务数据——采集合规性——行业标准解读
  13. Hadoop 中 FileSplit (文件分割器)的简单使用
  14. OpenCV-Python击中击不中HITMISS形态变换详解
  15. 浅析C++外部链接和内部链接
  16. 并联机器人开题报告怎么写_华东交通大学机械工程专硕培养方案怎么样?
  17. Mysql8.0.28-winx64安装
  18. 高通thermal-engine配置文件格式详解
  19. Windows Server 2008 R2 SP1中的具体改进
  20. c语言联想输入法算法,华为:编程实现联想输入法 输入联想功能是非常实用的一个功能,请编程实现类似功能...

热门文章

  1. 爱奇艺NLP:BiLSTM_CRF的关键词自动抽取
  2. python实现GBDT算法的回归、二分类以及多分类,算法流程解读并可视化
  3. gpio stm8 管脚 配置工具_STM8S 外设模块的GPIO引脚应该如何配置
  4. typecho运行html插件,typecho主题集成HTML压缩功能
  5. Self-Supervised Curriculum Learning for Spelling Error Correction
  6. 统计学习方法 第八章总结
  7. linux mysql5.5.50_linux下安装mysql5.5
  8. 《MFC游戏开发》笔记八 游戏特效的实现(二):粒子系统
  9. 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画
  10. 用Arduino远程控制车库门开关