leetcode_c++:树:Binary Tree Maximum Path Sum(124)
来源:互联网 发布:matlab 画数组 编辑:程序博客网 时间:2024/06/08 10:30
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 does not need to go through the root.
For example:
Given the below binary tree,
1 / \ 2 3
return 6
算法
求一棵二叉树上的最大路径。
直接 DFS 就可以了,返回以这一棵子树且一端在上的最大路径,然后维护一个最大路径就行了。
class Solution {private: int ret; int dfs(TreeNode *root) { if (root == NULL) return 0; int left_sum = max(0, dfs(root->left)); int right_sum = max(0, dfs(root->right)); ret = max(ret, left_sum + right_sum + root->val); return max(left_sum, right_sum) + root->val; }public: int maxPathSum(TreeNode *root) { ret = INT_MIN; dfs(root); return ret; }};
0 0
- leetcode_c++:树:Binary Tree Maximum Path Sum(124)
- LeetCode 124: Binary Tree Maximum Path Sum
- LeetCode 124 Binary Tree Maximum Path Sum
- LeetCode: Binary Tree Maximum Path Sum [124]
- 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
- 124 Binary Tree Maximum Path Sum
- 124 Binary Tree Maximum Path Sum [Leetcode]
- 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
- iOS 网络编程 (一)网络状态
- Android最大方法数和解决方案
- faster-rcnn在ubuntu环境下的配置以及demo运行
- TextUtils类的常用方法isEmpty与equals
- jdbc使用prepareStatement实现简单的转账功能
- leetcode_c++:树:Binary Tree Maximum Path Sum(124)
- C++中this指针
- MySQL数据类型
- RDD、DataFrame和DataSet的区别
- Nginx/LVS/HAProxy负载均衡软件的优缺点详解
- 什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?
- 13 Fibonacci数
- 普通伸展树(带注释版)
- Fourier 变换的分析和理解