LeetCode OJ-- Binary Tree Maximum Path Sum ***
https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/
给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的。求路径上最大和。
一般来说,路径是 V 字状的, 递归解的话, 每个点记录它的 左右子树的最大 V 值,并且和其的 V值相比较, 选最大的那个返回。
同时,为了计算它自己的 V 值,需要保留着,它左子树的 最大 path和,右子树的最大 path 和,这个path指的是 直的,没拐弯的。
注意,要对路径的值为负数的时候的处理。
/**
* 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) {
if(root == NULL)
return ; int ans = ;
calcV(root, ans);
return ans;
}
// return data is the biggest path
int calcV(TreeNode* root, int ¤t_biggest_v)
{
if(root == NULL)
{
current_biggest_v = ;
return ;
}
if(root->left == NULL && root->right == NULL)
{
current_biggest_v = root->val;
return root->val;
}
int left_biggest_v;
int right_biggest_v; int left_path = calcV(root->left, left_biggest_v);
int right_path = calcV(root->right, right_biggest_v); int ret_val = ; ret_val = max(left_path, right_path);
ret_val = max(ret_val, );
ret_val = ret_val + root->val; // with ? statements, with should add bracks
int current_v = (left_path>? left_path:) +( right_path>? right_path:) + root->val; if(root->left && root->right == NULL)
current_biggest_v = max(left_biggest_v, current_v);
else if(root->right && root->left == NULL)
current_biggest_v = max(right_biggest_v, current_v);
else
{
current_biggest_v = max(left_biggest_v, right_biggest_v);
current_biggest_v = max(current_biggest_v, current_v);
} return ret_val;
}
};
LeetCode OJ-- Binary Tree Maximum Path Sum ***的更多相关文章
- 【leetcode】Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- leetcode@ [124] Binary Tree Maximum Path Sum (DFS)
https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- Java for LeetCode 124 Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- LeetCode 124. Binary Tree Maximum Path Sum 二叉树中的最大路径和 (C++/Java)
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 【leetcode】Binary Tree Maximum Path Sum (medium)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- spark入门: wordcount-java
wordcount-java: pom.xml文件如下: <dependencies> <dependency> <groupId>junit</groupI ...
- python语言介绍
Python诞生于1989年,作者是吉多.范罗苏姆,人称龟叔,由C语言实现的. 1999年,基于python的web框架Zope 1诞生,标志着python向web领域迈出了第一步,现在这个框架好像不 ...
- Xenia and Bit Operations CodeForces - 339D
Xenia and Bit Operations CodeForces - 339D Xenia the beginner programmer has a sequence a, consistin ...
- POJ1426-Find The Multiple(搜索)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42035 Accepted: 176 ...
- [Poj3281]Dining(最大流)
Description 有n头牛,f种食物,d种饮料,每头牛有nf种喜欢的食物,nd种喜欢的饮料,每种食物如果给一头牛吃了,那么另一个牛就不能吃这种食物了,饮料也同理,问最多有多少头牛可以吃到它喜欢的 ...
- ACM二分搜索中的最大化最小值 总结
这类题目都有个相似的地方就是需要你去找一个临界点. 分析题目要你求什么,例如时间 那么mid就是时间 看求得这个跟什么相关 例如 poj 3258 求得是距离 这个距离跟两者之间的差相关 那题目要求你 ...
- 用私有构造器或者枚举类型强化Singleton属性
1.Singleton指仅仅被实例化一次的类.Singleton通常被用来代表那些本质上唯一的系统组件,如窗口管理器或者文件系统.使类称为Singleton会使它的客户端调试变的十分困难,因为无法给S ...
- #include "*.c"文件的妙用
在看uCOS II V2.91版本源代码时,在ucos_ii.c源文件中发现下面的代码: #include <os_core.c> #include <os_flag.c> # ...
- webpack + babel + vue 环境设置
npm i webpack --save-dev npm install style-loader css-loader url-loader babel-loader sass-loader fil ...
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...