【LeetCode】124. 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
.
树结构显然用递归来解,解题关键:
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相关推荐
- leetcode @python 124. Binary Tree Maximum Path Sum
题目链接 https://leetcode.com/problems/binary-tree-maximum-path-sum/ 题目原文 Given a binary tree, find the ...
- 【重点】LeetCode 124. Binary Tree Maximum Path Sum
LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...
- 124 Binary Tree Maximum Path Sum
题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...
- LeetCode 124. Binary Tree Maximum Path Sum
原题 求二叉树的最大路径和 Given a binary tree, find the maximum path sum. For this problem, a path is defined as ...
- 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 ...
- 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和
Title 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 ...
- Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum 题目链接:https://leetcode.com/problems... dfs对每个node,查一下包含这个node的最大路径值. /** ...
- 【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 ...
- 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. ...
最新文章
- 对Linux系统中的时钟和时间的探讨
- tensorflow.python.framework.errors_impl.UnknownError: Failed to get convolution algorithm.的一种解决方法
- 六、springboot(三)配置双数据源
- java 传递bean_如何将bean作为参数传递给JSP标记?
- java文本域 图片_java swing中给面板或者文本域设置背景图片的方法!
- 开启ssh不能用root登入的限制
- DeepFake技术--Deepfakes教程及各个换脸软件下载
- 电源大师课笔记 1.1
- LCD12864图形点阵液晶显示模块中文资料介绍
- 应用密码学(张仕斌)第四章
- unity3d手游破解(一)
- 车联网信息服务数据——采集合规性——行业标准解读
- Hadoop 中 FileSplit (文件分割器)的简单使用
- OpenCV-Python击中击不中HITMISS形态变换详解
- 浅析C++外部链接和内部链接
- 并联机器人开题报告怎么写_华东交通大学机械工程专硕培养方案怎么样?
- Mysql8.0.28-winx64安装
- 高通thermal-engine配置文件格式详解
- Windows Server 2008 R2 SP1中的具体改进
- c语言联想输入法算法,华为:编程实现联想输入法 输入联想功能是非常实用的一个功能,请编程实现类似功能...
热门文章
- 爱奇艺NLP:BiLSTM_CRF的关键词自动抽取
- python实现GBDT算法的回归、二分类以及多分类,算法流程解读并可视化
- gpio stm8 管脚 配置工具_STM8S 外设模块的GPIO引脚应该如何配置
- typecho运行html插件,typecho主题集成HTML压缩功能
- Self-Supervised Curriculum Learning for Spelling Error Correction
- 统计学习方法 第八章总结
- linux mysql5.5.50_linux下安装mysql5.5
- 《MFC游戏开发》笔记八 游戏特效的实现(二):粒子系统
- 《MFC游戏开发》笔记六 图像双缓冲技术:实现一个流畅的动画
- 用Arduino远程控制车库门开关