博客
关于我
LeetCode-Binary Tree Maximum Path Sum
阅读量:792 次
发布时间:2023-01-31

本文共 875 字,大约阅读时间需要 2 分钟。

哎,又是做失败的一题,果然遇到这种需要清晰的头脑把问题用最简单的方法描述解答出来的题目就会很悲剧,写代码之前没有

把问题用很简化的解法想清楚,写出来的bug不断,需要把最大路径和与节点的最大路径和分开来考虑!另外也印证了一条定理:

那就是面试的题目如果代码写得超过50行,就要停下来仔细思考一下了,因为很可能是由于误入歧途了,再在原有的思路上继续

非常可能是白白浪费时间!哎,泪奔~

1 class Solution { 2 public: 3     int res; 4     int getMax(TreeNode *node) { 5         if (node == NULL) { 6             return 0; 7         } 8         int left = getMax(node->left); 9         int right = getMax(node->right);10         int sum = max(node->val, max(left, right) + node->val);11         res = max(res, sum);12         res = max(res, left + right + node->val);13         return sum;14     }15     int maxPathSum(TreeNode *root) {16         // Start typing your C/C++ solution below17         // DO NOT write int main() function18         res = INT_MIN;19         getMax(root);20         return res;21     }22 };

 

转载于:https://www.cnblogs.com/chasuner/p/maxpathsum.html

你可能感兴趣的文章
leetcode题解767-重构字符串
查看>>
leetcode题解77-子集
查看>>
leetcode题解77-组合
查看>>
leetcode题解776-旋转字符串
查看>>
leetcode题解8-盛最多水的容器
查看>>
leetcode题解976-三角形的最大周长
查看>>
leetcode题解98-验证二叉搜索树
查看>>
LeetCode题解【打家劫舍】(中等难度)
查看>>
Leetcode题解(二)
查看>>
left join on、where后面的条件的区别
查看>>
left join right inner join 区别
查看>>
leftjoin多个on条件_MySQL:left join 避坑指南
查看>>
legend2---开发日志3(thinkphp的入口目录是public的体现是什么)
查看>>
legoblock秀上限
查看>>
LeNet介绍-ChatGPT4o作答
查看>>
LeNet剪枝
查看>>
Length of Last Word
查看>>
Lenovo E47A Ubuntu闪屏解决办法
查看>>
Leopard系统装好后不能从硬盘引导的朋友看过来
查看>>
Lepus搭建企业级数据库全方位监控系统
查看>>