[Leetcode] Binary Tree Maximum Path Sum
这是LeetCode上的一道题目,需要求二叉树中两点路径的最大和。原题是
https://oj.leetcode.com/problems/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
.
大概的想法其实很简单,类似于用分治法求数组连续和的最大值。最大值有三种可能,第一种是起点和终点最大和在左子树中,第二种是起点和终点最大和在右子树中,第三种是起点在左子树中,终点在右子树中。第三种要求路径必须通过根节点,因此其左右子树中的路径只能是从左右子树的根节点到其中的某个节点。根据这个思路,就可以写出如下的代码。
class Solution { public:int maxPathSum(TreeNode *root) {if(root==NULL)return 0;else{if(root->left==NULL && root->right==NULL)return root->val;int maxLeft = maxPathSum(root->left);int maxRight = maxPathSum(root->right);int maxLeafLeft = max(0, maxLeafSum(root->left));int maxLeafRight = max(0, maxLeafSum(root->right));int sumWithRoot = root->val + maxLeafLeft + maxLeafRight;return max(maxLeft, maxRight, sumWithRoot);} }int maxLeafSum(TreeNode* root){if(root==NULL)return 0;else{if(root->left==NULL && root->right==NULL)return root->val;else{return root->val + max(maxLeafSum(root->left), maxLeafSum(root->right)); } } } }
这个方法虽然结果是正确的,但是很不幸严重超时。虽然我知道瓶颈是出在maxLeafSum()上,这其中有很多的冗余计算,但是没有想到该如何优化。后来看了别人的解答,才发现自己对递归的理解还是不够深入。我参考了soulmachine的解答(顺便说一下,这个解答是我看过的最详细的解答了 https://github.com/soulmachine/leetcode ),他用的代码是
class Solution {public:int maxPathSum(TreeNode *root) {max_sum = INT_MIN;dfs(root);return max_sum;} private:int max_sum;int dfs(const TreeNode *root) {if (root == NULL) return 0;int l = dfs(root->left);int r = dfs(root->right);int sum = root->val;if (l > 0) sum += l;if (r > 0) sum += r;max_sum = max(max_sum, sum);return max(r, l) > 0 ? max(r, l) + root->val : root->val;} };
这里的DFS就是返回从根节点到某个子节点的路径最大和。咋看一下似乎不对,怎么只考虑了路径通过根节点的情形呢,即开头我们讨论的第三种情形?这个地方的巧妙之处在于,我们所要求的最大和路径必定是满足其起点和终点在某个节点的两侧。所以只要我们用某个把这个函数应用到所有节点之上,把每次的计算结果与一个全局变量比较,就可以得到正确的结果。
非常巧妙的方法!这也说明递归有时候可以简化问题,甚至想得越简单越好,只要我们保证最优解肯定对于某个节点满足我们给定的形式。
用类似的办法可以计算二叉树所有节点中的最大值、最小值。
转载于:https://www.cnblogs.com/darkphoton/p/4141417.html
[Leetcode] 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. ...
- 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.Fo ...
- 【重点】LeetCode 124. Binary Tree Maximum Path Sum
LeetCode 124. Binary Tree Maximum Path Sum 参考链接:http://zxi.mytechroad.com/blog/tree/leetcode-124-bin ...
- 【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 ...
- Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum 题目链接:https://leetcode.com/problems... dfs对每个node,查一下包含这个node的最大路径值. /** ...
- 124 Binary Tree Maximum Path Sum
题目: 124 Binary Tree Maximum Path Sum 这道题就是分别算出左子树和右子树的可能最大和,然后对Path的值进行更新即可 class Solution:def __ini ...
- 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
原题 求二叉树的最大路径和 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 ...
最新文章
- mysql负载均衡与同步_MySql数据库从同步负载均衡实时备份
- 64位jvm的指针膨胀 和 数据补白
- HDU 3282 Running Median 动态中位数,可惜数据范围太小
- 修改服务器的时区为gmt,将GMT转换为服务器本地时区C#
- 超级详细的教程 一步步教你Vue项目中使用axios如何进行参数拼接
- python设置图片透明度_学习python第40天
- mysqlierror php_php操作mysqli(示例代码)
- 今天研究 Client本来是关联的Expression接口,笔记记录一下。
- jQuery的实现,去掉传入html代码两端的空格:
- 一文了解 2018 年最值得关注的 12 大框架
- python 处理xml pandas_在python中解析xml到pandas数据帧
- 论需求分析方法及应用--系统分析师
- Android安卓身份证识别SDK
- 字节跳动python后端_【字节跳动】[字节跳动][实习]后端研发工程师(python+go)...
- GoLand+Delve(dlv) 远程调试
- 第五十五讲 插件设备树
- 天猫618红包口令怎么获取?天猫618红包使用条件有哪些?
- 解决:2003-Cant connect to MySQL server on **** 以及use near ‘IDENTIFIED BY ‘*****‘ WITH GRANT OPTION‘ at
- 基于opencv的图像的无失真放大
- 【操作系统-进程】PV操作——吸烟者问题