第四周 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 ...
随机推荐
- 关于iframe与$.load()哪个更好
iframe与$.load()哪个更好 iframe可以直接加载页面,但是要付出降低搜索引擎搜索效率的代价,它引入静态文件的方式是完全独立的,简单意思就是,在页面一(父级页面)用ifram ...
- Ansible基于playbook批量修改主机名实战
Ansible基于playbook批量修改主机名 安装Ansible,相信这里也不用多说,大家都知道 说一下环境:这里的主机名是修改之后的,我先把其他两台的主机名改为别的 192.168.30.21 ...
- Mkdocs在html网页上看markdown
目录 Mkdocs在html网页上看markdown 1. 本文目的 2. Mkdocs介绍 3. DEMO的演示 3.1 配置需求 3.2 安装mkdocs 3.3 新建工程 3.4 启动服务器 3 ...
- 22万个木箱!TWaver 3D极限压榨
打开个门户网站都千呼万唤,我们还能期待网页上的3D技术会有“酣畅淋漓”.“一气呵成”的感受吗?也许现在还差点火候.但是HTML5.WebGL等技术一直在飞速的发展,可能很快你就会惊讶它的能力.现在,我 ...
- Oracle中的COALESCE,NVL,NVL2,NULLIF函数
http://jingyan.baidu.com/article/fa4125acaf898e28ac7092b9.html
- java基础学习日志--Stirng内存案例
案例一: public class test1 { public static void mb_swap(String Str1,String Str2) { String temp=Str1; St ...
- x shell 连接不上本地虚拟机
登陆虚拟机服务器 输入ipconfig查看ip 地址(如果提示命令不存在,输入 ip addr) 输出结果中看标记处是否出现ip地址.我的打开后这里是没有地址的 然后输入 vi /etc/syscon ...
- oracle 清理跟踪文件.trc .trm
trc,trm文件介绍:trc:系统的跟踪文件(trace),当系统启动时或运行过程中出现错误时,系统会自动记录跟踪文件到指定的目录,以便于检查,这些文件需定期维护删除.trm:伴随着.trc文件产生 ...
- PAT 1079. 延迟的回文数
PAT 1079. 延迟的回文数 给定一个 k+1 位的正整数 N,写成 ak...a1a0 的形式,其中对所有 i 有 0 <= ai < 10 且 ak > 0.N 被称为一个回 ...
- 【Codeforces 348A】Mafia
[链接] 我是链接,点我呀:) [题意] 每轮游戏都要有一个人当裁判,其余n-1个人当玩家 给出每个人想当玩家的次数ai 请你求出所需要最少的玩游戏的轮数 使得每个人都能满足他们当玩家的要求. [题解 ...