[leetcode]124. Binary Tree Maximum Path Sum
来源:互联网 发布:windows live安装包 编辑:程序博客网 时间:2024/05/29 15:43
题目链接:https://leetcode.com/problems/binary-tree-maximum-path-sum/#/description
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
.
class Solution {public: int maxPathSum(TreeNode* root) { int maxPath=INT32_MIN; dfsMaxPath(root,maxPath); return maxPath; } int dfsMaxPath(TreeNode* root,int &maxPath) { if(!root) return 0; int l=max(0,dfsMaxPath(root->left,maxPath)); int r=max(0,dfsMaxPath(root->right,maxPath)); maxPath=max(maxPath,l+r+root->val); return root->val+max(l,r); }};
阅读全文
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
- 1211: 中文系素素的困惑
- android点击button后的页面跳转
- 树状结构显示之递归练习并说明写递归代码的技巧
- 自定义标签配置详解
- 《算法分析与设计》Week 19
- [leetcode]124. Binary Tree Maximum Path Sum
- leetcode371题解
- 矩阵(matrix)应用大总结(二)HDU1588+HDU 5950+HDU 5015
- JSP简介——9大隐式对象
- RabbitMQ 三 角色权限
- Linux中制作压缩文件
- Linux学习总结(六)
- 计数排序
- Python基础 print()莫烦视频