Leetcode 124. Binary Tree Maximum Path Sum
来源:互联网 发布:java购物商城源码 编辑:程序博客网 时间:2024/06/05 15:12
- Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum.
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6.
//递归时注意分清左右子树的最大单向路径和全局最大路径(可以从左子树到父亲节点再到右子树)/** * 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 help(TreeNode* root,int &m){ if(root==0){ return 0; } int left=help(root->left,m); int right=help(root->right,m); int ret=max(max(left,right),0)+root->val; m=max(max(m,ret),left+right+root->val); return ret; } int maxPathSum(TreeNode* root) { if(root==0) return 0; int ret=root->val; help(root,ret); return ret; }};
阅读全文
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] 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
- 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
- LeetCode 124. Binary Tree Maximum Path Sum
- LEETCODE 124. Binary Tree Maximum Path Sum
- 算法博弈:机制设计
- 【计算机网络】网络诊断工具ping的模拟实现之搭建框架
- poj3252(数位dp)
- 模板方法设计模式
- [leetcode]565. Array Nesting
- Leetcode 124. Binary Tree Maximum Path Sum
- 常用类
- anaconda 下多版本Python 安装说明
- 个人不熟悉的面试题目(c++,数据结构)
- 剑指offer--面试题28:对称的二叉树
- 实习遇到的坑....
- hdu 3549 isap
- jQuery中foreach的continue和break : Uncaught SyntaxError: Illegal continue statement
- MySQL数据库二(数据表创建以及插入数据)