Binary Tree Maximum Path Sum
来源:互联网 发布:nginx动静分离原理 编辑:程序博客网 时间:2024/06/06 06:07
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
.
维护一个最大数,同时对于每一个root,都找出他所能贡献的最大分支。
/** * 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) { myMax=INT_MIN; maxPathHelper(root); return myMax; } int maxPathHelper(TreeNode *root){ if (!root) return 0; int l=max(0, maxPathHelper(root->left)); int r=max(0, maxPathHelper(root->right)); myMax= max(l+r+root->val, myMax); return max(l,r)+root->val; } int 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
- 学习NodeMCU的低功耗休眠
- UIApplicationDelegate协议解读
- 二叉树线索化
- linux运维职业路线
- Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法
- Binary Tree Maximum Path Sum
- 后海日记(1)
- ArcGIS教程:3D表面的基础知识(二)
- Android中Service类onStartCommand
- leetcode 203 Remove Linked List Elements
- yum-axelget setup in centos 6.x
- github 使用之project 创建
- Oracle 日期格式化处理汇总
- 谷俊丽:基于大数据的深度学习