[Leetcode] Max Tree Sum
来源:互联网 发布:销售出入库软件 编辑:程序博客网 时间:2024/06/05 22:32
不多说了~
/** * 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() function if(!root){ return 0; } int maxSumVal = INT_MIN; currentMaxValue(root,maxSumVal); return maxSumVal; } int currentMaxValue (TreeNode *root,int& sum) { if(!root){ return 0; } int rootSelf = root->val; int rootLeft = currentMaxValue(root->left,sum); int rootRight = currentMaxValue(root->right,sum); int rootLeft2right = rootLeft + rootRight + rootSelf; int localMaxVal = max(max(rootLeft+rootSelf,rootRight+rootSelft),max(rootLeft+rootSelf,rootSelf)); sum = max(max(sum,localMaxVal),max(sum,rootLeft2right)); return localMaxVal; }};
- [Leetcode] Max Tree Sum
- Binary Tree Max Path Sum
- leetCode: binary tree sum
- LeetCode[Tree]: Path Sum
- **(leetcode) (tree) Path Sum II
- LeetCode[Tree]: Path Sum II
- [leetcode][tree][dfs] Path Sum
- 【LeetCode】Binary Tree Leaf Sum
- 【leetcode】Min & Max Depth of Binary Tree
- max/min Depth of Binary Tree - LeetCode
- *****(leetcode) Binary Tree Maximum Path Sum (tree)
- 二叉树最大路径和 Binary Tree Max Path Sum
- Finding max & sum of a tree in Haskell with map
- Max Sum
- Max Sum
- Max Sum
- max sum
- Max Sum
- Libgdx Developer's Guide(Libgdx开发者手册)-4(应用框架)
- Android编译,模块的编译和CLEAN
- js判断是否为空
- SQL Server数据库中批量替换数据的方法
- MBean的实现
- [Leetcode] Max Tree Sum
- windows下lib与dll关系
- HDOJ 1159 Common Subsequence 杭电 ACM
- Struts2从后台传递数据到前台的主要方法和流程
- 计算字符串之间的相似度(递归)
- Ubuntu 12.04下安装中文分词搜索Coreseek
- Go编译选项(笔记)
- sicily 1001. Alphacode
- Unbound classpath container: 'JRE System Library [jdk1.6.0_04]' in project