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解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
随机推荐
- Flex 调用webService
今天手头没事,就学习下 Flex 调用webService的方法.本地测试OK 和大家分享下. ——————————————————————————————————————————————————— ...
- MVC 自定义AuthorizeAttribute 实现权限验证
MVC内置的AuthorizeFilter先于Action/Result过滤器执行,为网站权限验证提供了很好的一套验证机制. 通过自定义的AuthorizeAttribute可以实现对用户权限的验证. ...
- 解决Oracle ORA-00984: column not allowed here
某列是字符列,结果忘记加单引号了 INSERT INTO prt_document_present (company_code, doc_no, seq_no, field_name, desc_ms ...
- ural 1160
最小生成树 第一次敲 套用几个函数 其实挺容易的 #include <cstdio> #include <cstring> #include <vector> # ...
- Scala学习——基础篇
[<快学Scala>笔记] 一.基础 1.变量val 标志符: 声明常量: 如,val answer = 1var 标志符:声明变量: 类型推断:变量的类型由scala根据初始化变量的表达 ...
- [itint5]区间相交
http://www.itint5.com/oj/#14 要记录原来的索引,所以用了额外的空间,新生成一个结构.如果要省空间,可以用指针来排序,最后拿指针减去索引0的位置就是index,见:http: ...
- linux下使用yum安装mysql、tomcat、httpd
一.linux下使用yum安装mysql 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep m ...
- 这些废弃的 HTML 标签不要用
HTML 已经发展了多年,现在 W3C 已经发布了 HTML 5.1 的提案推荐标准,一些陈旧废弃的标签已经在后继的标准中逐渐消失.这里为大家列出那些已经被废弃 HTML 标签,看看你是不是还在使用它 ...
- Foreman--管理PuppetClient
一. 环境: 1. server: puppetmaster+activemq+foreman1.3 server1.xxx.com(10.8.1.201) 2. client: fedora 19 ...
- VMWare-NAT模式实现局域网其他主机对虚拟机访问
WIN 2012在桥接模式下可以实现主机及主机所在局域网内其他主机对虚拟机的访问,但是在NAT模式下主机可以对虚拟机访问,但是主机所在的局域网内其他主机却无法对虚拟机访问,必须进行主机转发,从而实现局 ...
