第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum
题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和。
思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow。
然后考虑经过这个节点的情况来更新最终答案。更新答案后返回Vnow供父节点继续更新。
代码很简单.
有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大。参考POJ3728
class Solution {
public:
int ans;
Solution(){ans=-2147483647;}
int maxPathSum(TreeNode* root) {
solve(root);
return ans;
}
int solve(TreeNode* root) {
if(root->left==NULL&&root->right==NULL)
{ans=max(ans,root->val);return root->val;}
int vnow=root->val;ans=max(ans,vnow);
int lv=0,rv=0;
if(root->left!=NULL)
{
lv=solve(root->left);
vnow=max(vnow,root->val+lv);
ans=max(ans,vnow);
}
if(root->right!=NULL)
{
rv=solve(root->right);
vnow=max(vnow,root->val+rv);
ans=max(ans,vnow);
}
ans=max(ans,lv+rv+root->val);
return vnow;
}
};
第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)的更多相关文章
- 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 求二叉树的最大路径和
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 (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 ...
- 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 124. Binary Tree Maximum Path Sum ----- java
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 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
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
随机推荐
- ThinkPHP---thinkphp拓展之空操作
[一]概论 (1)定义 空操作指系统在找不到指定操作方法的时候.会定位到空操作方法 / 控制器来执行,利用这个机制,我们可以实现错误页面的自定义和URL的优化 (2)场景 常用于错误页面的自定义 (3 ...
- 百度地图API获取数据
目前,大厂的服务范围越来越广,提供的数据信息也是比较全的,在生活服务,办公领域,人工智能等方面都全面覆盖,相对来说,他们的用户基数大,通过用户获取的信息也是巨大的.除了百度提供api,国内提供免费AP ...
- [C++] 化学方程式的格式化算法
网上普遍使用的化学方程式的格式普遍如下 例: KMnO4+FeSO4+H2SO4=Fe2(SO4)3+MnSO4+K2SO4+H2O 要把化学方程式格式化,单单一个正则表达式是非常反人类的,故可选用 ...
- UVA - 247 Calling Circles(Floyd求传递闭包)
题目: 思路: 利用Floyd求传递闭包(mp[i][j] = mp[i][j]||(mp[i][k]&&mp[k][j]);),当mp[i][j]=1&&mp[j][ ...
- shell日志颜色处理
记录一下shell日志颜色处理 _COLORS=${BS_COLORS:-$(tput colors >/dev/)} __detect_color_support() { # shellche ...
- Yii 时间日期组件与composer 下载中出现的问题
首先本篇主要讲3点 一个Yii时间日期组件的两种用法 笔者使用composer下载该组件时出现问题的解决办法 1.composer下载出现的问题 file could not be downloade ...
- spring boot 的使用(一)
1. 启动spring-boot项目 mvn spring-boot:run cd target java -jar xxxx.jar xxxx代表生成的jar包
- Convolutions in TensorFlow
Convolutions in TensorFlow Convolutions without training You might already be familiar with the term ...
- poj 1562 简单深搜
//搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...
- WinForm 中限制只能输入数字
在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值.限制只能输入数字,小数点,Backspace,del这几个键.数字0~9所对应的 ...