Binary Tree Maximum Path Sum
来源:互联网 发布:mac清除软件缓存 编辑:程序博客网 时间:2024/06/05 14:35
Given a binary tree, find the maximum path sum.
The path may start and end at any node in the tree.
For example:
Given the below binary tree,
1 / \ 2 3
Return 6
.
#include<iostream>#include<vector>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};int mymax = INT_MIN;int maxSum(TreeNode *root) {if (root==NULL)return 0;int leftmax = maxSum(root->left);int rightmax = maxSum(root->right);int Value = 0;if (leftmax>0)Value+=leftmax;if (rightmax>0)Value+=rightmax;mymax = Value>mymax?Value:mymax;return max(root->val,max(leftmax+root->val,rightmax+root->val));}int maxPathSum(TreeNode *root){if (root==NULL)return 0;maxSum(root);return mymax;}
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
- Beyond Frontal Faces: Improving Person Recognition Using Multiple Cues
- JavaScript客户端存储
- 求解次方的次方。pow(a,pow(b,c))%p
- STL中的全排列(2)
- C++常见面试题:虚函数实现原理
- Binary Tree Maximum Path Sum
- Fence Repair
- java实现文件上传到数据库
- Android中Intent的深入理解
- javascript ++i 与 i++ 的区别 ?
- 欢迎使用CSDN-markdown编辑器
- 关于Swiper3.x的a标签bug
- hdu 5195 BC#35 拓扑排序 优先队列 重复入队的想法 十字链表
- Android点击事件的实现