leetcode–Binary Tree Maximum Path Sum
1.题目说明
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 3Return 6.
2.解法分析:
leetcode中给出的函数头为:int maxPathSum(TreeNode *root)
给定的数据结构为:
Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };乍一看这道题我就递归,每一条路径都会有一个最高节点,整棵树的最高节点是root,因此,对整棵树而言,和最长的路径只有三种情况:
- 路径的最高节点为root
- 路径的最高节点在root的左子树中
- 路径的最高节点在root的右子树中
所以,这题可以递归来做,需要考虑的是路径中至少有一个节点,不能是空路径,这会给编码带来一定的麻烦,而且,虽然有了刚才的三个分类,怎么求三种情况下的最长路径呢?我们定义从节点A往下走一直到根部(可以不到根部)的路径中和最大的这个值为rootStartPathMaxSum(A),那么必然有,:
- 如果路径的最高节点经过了root:理论上最大值为max(0,rootStartPathMaxSum(root->left) )+max(0,rootStartPathMaxSum(root->right) ) +root->val;
- 如果路径的最高节点在root,递归计算
- 如果路径的最高节点在root右侧,递归计算
最后比较这三种得出的值即可。
rootStartPathMaxSum(TreeNode *)这个函数的计算我最开始的算法是递归的。于是得出了下面一份代码。
/*** 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 maxPathSum(TreeNode *root) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(root == NULL)return 0;if(root->left == NULL && root->right == NULL){return root->val;}int case_both_side = max(0,rootStartPathMaxSum(root->left))+max(0,rootStartPathMaxSum(root->right))+root->val;if(root->left!=NULL && root->right == NULL){return max(case_both_side,maxPathSum(root->left));}if(root->left==NULL && root->right != NULL){return max(case_both_side,maxPathSum(root->right));}elsereturn max(max(maxPathSum(root->left),maxPathSum(root->right)),case_both_side);}// 从root开始往根出发的和最长路径,不一定要到达根部int rootStartPathMaxSum(TreeNode *root){if(root == NULL)return 0;if(root->left == NULL&& root->right == NULL)return root->val;if(root->left == NULL && root->right != NULL){return max(root->val,root->val+rootStartPathMaxSum(root->right));}if(root->left != NULL && root->right ==NULL){return max(root->val,root->val+rootStartPathMaxSum(root->left));}return max(max(rootStartPathMaxSum(root->left)+root->val,rootStartPathMaxSum(root->right)+root->val),root->val);}};
在小数据集上运行良好,但是一到大数据集就hold不住了,运行结果如下:
其实写的过程就意识到了rootStartPathMaxSum有很多次被重复调用,于是得采用一种自底向上的算法,自己想了半天没想出来,结果网上搜到了一个神代码,我承认,很精妙,记录一下,学习一下:
/*** 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 maxPathSum(TreeNode *root) {// Start typing your C/C++ solution below// DO NOT write int main() functionif (root == NULL)return 0;int max = root->val;getPathSum(root, max);return max;}private:int getPathSum(TreeNode *root, int &max) {if (root == NULL)return 0;int leftSum = getPathSum(root->left, max);int rightSum = getPathSum(root->right, max);if (leftSum + root->val + rightSum > max)max = leftSum + root->val + rightSum;int subPathSum = leftSum > rightSum ? leftSum : rightSum;subPathSum += root->val;return subPathSum > 0 ? subPathSum : 0;}};转载自:http://blog.csdn.net/niaokedaoren/article/details/8798528
总的来说,我的算法思路跟这位是一样的,可惜实现思路的功底却差了很多,加油!
后记: 回去略微思索,上述思路中用一个max记录了当前最大值,leftsum和rightSum正是我所想追求的自底向上的中间变量,学习了,不过我的算法的有点事可以用两个中间变量保存起点和终点,这样就有利于路径记录。
leetcode–Binary Tree Maximum Path Sum的更多相关文章
- [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 ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [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. ...
- C++ leetcode Binary Tree Maximum Path Sum
偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...
- [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 @ Python
原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 题意: Given a binary tree, find th ...
- [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 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- 【弱省胡策】Round #5 Construct 解题报告
这个题是传说中的 Hack 狂魔 qmqmqm 出的构造题.当然要神. 这个题的本质实际上就是构造一个图,然后使得任意两点间都有长度为 $k$ 的路径相连,然后对于任意的 $i < k$,都存在 ...
- Python利用ConfigParser读取配置文件
http://www.2cto.com/kf/201108/100384.html #!/usr/bin/python # -*- coding:utf-8 -*- import ConfigPars ...
- 程序自动生成Dump文件
前言:通过drwtsn32.NTSD.CDB等调试工具生成Dump文件, drwtsn32存在的缺点虽然NTSD.CDB可以完全解决,但并不是所有的操作系统中都安装了NTSD.CDB等调试工具.了解了 ...
- CodeForces 279B Books
http://codeforces.com/problemset/problem/279/B 题意 :Valera 有很多的空闲时间,所以他决定看书,给出n本书,编号1到n,和看每本书需要的时间,他看 ...
- 【转】VMware设置共享文件夹之后Ubuntu中看不到怎么办?
一.共享文件夹设置好了,但是在虚拟机中的Ubuntu系统下却看不到,怎么办? 一种可能的原因是系统没有自动挂载,解决办法: 1.安装: sudo apt-get insta ...
- Altium Designer13 如何导出Gerber文件
参考<http://blog.sina.com.cn/s/blog_9b9a51990100zyyv.html> 版本:AD13.3.4 目的:Gerber文件导出备忘 目录: Step1 ...
- P140、面试题24:二叉搜索树的后序遍历序列
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 测试用例: 1)功能测试(输入的后序遍历的序列 ...
- ISE综合后得到的RTL图如何与硬件对应起来,怎么知道每个element的功能
2013-06-23 21:34:03 要知道“我写的这段代码会综合成什么样的电路呢”,就要搞清楚RTL图中每个模块的功能,从而将代码与硬件对应,判断综合后的电路是否与预期的一致.如何做到? 之前查了 ...
- MySQL复制应用中继日志解析
前言:SQL线程应用中继日志,在binlog_format是row格式的时候,是居于主键更新,下面结合一张图来证明 1.从一个大神那边得到一张图片,SQL线程应用中继日志流程,下面就实验验证一下:(P ...
- 关于 mysqladmin
>mysqladmin 工具的使用格式: mysqladmin [option] command [command option] command ...... option 选项: -c nu ...
