124_binaryTreeMaximumPathSum
来源:互联网 发布:淘宝网货源 编辑:程序博客网 时间:2024/05/16 10:35
/*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 3Return 6.自下而上,计算每个节点的左支路值与右支路值和本节点值,比较最大值并更新加上该节点的支路最大值(并与0比较,取大的)*//** * 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 maxPathSum(TreeNode* root) { int sum=INT_MIN; int tmp=findMax(root,sum); sum=max(tmp,sum); return sum; } int findMax(TreeNode* root,int &sum){ if(!root) return 0; int leftSum=max(0,findMax(root->left,sum)); int rightSum=max(0,findMax(root->right,sum)); sum = (leftSum+rightSum+root->val) > sum ? leftSum+rightSum+root->val : sum; return leftSum > rightSum ? leftSum+root->val : rightSum+root->val; }};
阅读全文
0 0
- 124_binaryTreeMaximumPathSum
- 124
- 124
- 124
- 124
- 124
- fs:[124]
- NYOJ - 124
- rqnoj-124
- 124 中位数
- ocp-124
- muShtaPmumixaMeerTyraniB.124
- 题目124
- codeforces 124B、124C
- SQLServer技术(124)
- reactos操作系统实现(124)
- fs:[124]对应CurrentThread
- Codeforces 124 E
- Micro Python微控制器
- 存储器、硬盘 内存 缓存 寄存器
- Java的动态绑定和静态绑定
- gets 和 scanf 的区别
- 多么痛的领悟---关于RMB数据类型导致的元转分分转元的bug
- 124_binaryTreeMaximumPathSum
- iOS WebView 图片点击放大并左右滑动,类似微信/网易文章功能
- bootstrap-datetimepicker时间控件
- JSP九大隐式对象
- 两个 Asp.Net Core 2.0 项目共享登录
- laravel验证码,自定义表单验证
- JDBC使用模板
- 设计模式之单例模式
- tensorflow分布式部署和开发