LeetCode(124) Binary Tree Maximum Path Sum (如何递归?)
来源:互联网 发布:www.杭州行知小学.com 编辑:程序博客网 时间:2024/06/06 20:05
转载 :http://blog.csdn.net/fly_yr/article/details/50412751
ac代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int INF = 0x7fffffff; int MIN_INF = -INF-1; int maxVal; int dfs(TreeNode* root) { if(root == NULL) return 0; else{ int leftM = dfs(root->left); int rightM = dfs(root->right); int rootVal = root->val; int cur = rootVal; if(leftM > 0) cur += leftM; if(rightM > 0) cur += rightM; if(cur > maxVal) maxVal = cur; return max(rootVal, max(rootVal+leftM, rootVal+rightM)); } } int maxPathSum(TreeNode* root) { if(root == NULL) return 0; maxVal = MIN_INF; dfs(root); return maxVal; }};
注意递归思路
0 0
- LeetCode(124) Binary Tree Maximum Path Sum (如何递归?)
- LeetCode(124) Binary Tree Maximum Path Sum
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode: Binary Tree Maximum Path Sum [124]
- LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode(124) Binary Tree Maximum Path Sum
- [LeetCode 124]Binary Tree Maximum Path Sum
- leetcode || 124、Binary Tree Maximum Path Sum
- LeetCode #124 Binary Tree Maximum Path Sum
- leetcode 124: Binary Tree Maximum Path Sum
- 124 Binary Tree Maximum Path Sum [Leetcode]
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode 124:Binary Tree Maximum Path Sum
- LeetCode 124 Binary Tree Maximum Path Sum
- Leetcode 124 Binary Tree Maximum Path Sum
- Leetcode #124 Binary Tree Maximum Path Sum
- Leetcode-124: Binary Tree Maximum Path Sum
- ios 计算两个时间之间的时间差
- HDU 1796:How many integers can you find(容斥原理)
- H - Marriage is Stable 母函数模版题
- 《挑战》这本书写的真不错
- Python编写简易脚本文件
- LeetCode(124) Binary Tree Maximum Path Sum (如何递归?)
- ORACLE数据库的监听日志文件莫名丢失
- 获取对话框的控件
- 总结32
- P1008传球游戏
- 嵌入式系统开发概述
- caffe_2016版本+windows+vs2013+matlab接口配置
- 小辣鸡尝试语义分析
- [LeetCode] 438. Find All Anagrams in a String