Binary Tree Maximum Path Sum
来源:互联网 发布:键位修改软件 编辑:程序博客网 时间:2024/06/07 15:07
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
class Solution {public: int maxPathSum(TreeNode* root) { if(!root) return 0; int m=-2147483648; maxPath(root,m); return m; } int maxPath(TreeNode* root,int &m){//这里的m要是引用 if(!root) return 0; int a=maxPath (root->left,m); int b=maxPath(root->right,m); if(a<0) a=0; if(b<0) b=0; if(a+b+root->val>m) m=a+b+root->val; return root->val+max(a,b); }};
0 0
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- Binary Tree Maximum Path Sum
- 21天精通java基础之Day13Map接口
- 常见Python运行错误
- 前端性能优化
- android app唤醒手机qq,发起即时对话
- Leetcode #541 Reverse String II
- Binary Tree Maximum Path Sum
- Spring中数据库连接池需要注意的问题
- 关闭全部动态加载的子集菜单
- Firefox新打开标签页不断刷新无法输入地址
- jQuery学习笔记
- 开发地址
- 怎么做二手车成交可能性预测
- Java跨平台原理
- Expected resource of type drawable [ResourceType]