LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)
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.
求二叉树的最大路径和,感觉好复杂,但是分析一下,由于中间的点不能重叠,所以说肯定一部分在某个点的左边一个在右边。所以可以遍历左右子树来求最大值,相当于遍历所有的单点以及其左右子树之后,不断的更新最大值,用ret全局变量来维护这个最大值。将总体当作根节点加上左边和右边就可以了,代码如下:
class Solution {
public:
int maxPathSum(TreeNode *root) {
if(!root) return ret;
ret = INT_MIN;
dfs(root);
return ret;
}
int dfs(TreeNode * node)
{
if(node == NULL) return ;
int val = node->val;
int left = dfs(node->left);
int right = dfs(node->right);
if(left > ) val += left;
if(right > ) val += right;
if(val > ret)
ret = val;
return max(node->val, max(node->val + left, node->val + right));
}
private:
int ret;
};
LeetCode OJ:Binary Tree Maximum Path Sum(二叉树最大路径和)的更多相关文章
- [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 Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)
题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...
- 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】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 求二叉树的最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [LeetCode] 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 binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 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 ...
随机推荐
- Java实现八大排序之冒泡排序
冒泡排序 冒泡排序的定义: 冒泡排序(Bubble Sort)它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该 ...
- kibana 创建饼图
1.在 visualize 里面选择饼图 2.选择数据源 3.选择 terms 聚合类型. 选择字段 xxx.keyword 保存即可.
- Linux学习笔记(9)linux网络管理与配置之一——Linux基础网络命令与学习大纲(0)
大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配置DNS客户端 4.配置名称解析顺序 5.配置路由与默认网关 6.双网卡绑定 [1] ping [2]net ...
- HBA 卡和RAID 卡
HBA卡: 只从HBA的英文解释HOST BUS ADAPTER(主机总线适配器)就能看出来,他肯定是给主机用的,一般HBA就是给主机插上后,给主机扩展出更多的接口,来连接外部的设备.大多数讲到HBA ...
- 吴超老师课程--Hbase介绍和伪分布式安装
1.HBase(NoSQL)的数据模型1.1 表(table),是存储管理数据的.1.2 行键(row key),类似于MySQL中的主键. 行键是HBase表天然自带的.1.3 列族(col ...
- delphi连接sql server的字符串2011-10-11 16:07
delphi连接sql server的字符串2011-10-11 16:07 一.delphi连接sql server 放一个连接组件 ADOConnection, 其它组件TADODataSet,T ...
- HDU - 6370 Werewolf 2018 Multi-University Training Contest 6 (DFS找环)
求确定身份的人的个数. 只能确定狼的身份,因为只能找到谁说了谎.但一个人是否是民,无法确定. 将人视作点,指认关系视作边,有狼边和民边两种边. 确定狼的方法只有两种: 1. 在一个仅由一条狼边组成的环 ...
- 发送邮件——stamplib
配置文email.ini件信息: [email]sender=xxxxxxxxxxxpwd=xxxxxxxxxxxxreciver=xxxxxxxxxxxxxpython 3.x代码如下: impor ...
- Firebug控制台详解(转)
本文转自:http://www.ruanyifeng.com/blog/2011/03/firebug_console_tutorial.html 作者: 阮一峰 日期: 2011年3月26日 Fir ...
- canvas笔记1
w3c定义: <canvas> 标签定义图形,比如图表和其他图像. <canvas> 标签只是图形容器,您必须使用脚本来绘制图形. canvas 对象 属性: width he ...