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 ...
随机推荐
- Windows下QT MySQL驱动编译
在Windows环境中使用Qt进行关于MySQL数据库的操作时,会出现如下问题: QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: availa ...
- 科班学习java遇到瓶颈,每天云里雾里怎么办?
声明:这个问题困扰了我好久,今天在知乎找到了答案.知乎链接https://www.zhihu.com/question/24240982,感谢大神@Tony He的回答. 作者:Tony He链接:h ...
- C语言自带快速排序对比插入排序
#include <stdio.h> #include <stdlib.h> #include <time.h> void getRandomArr (int ar ...
- Hadoop2.0NameNode HA和Federation实践
一.背景 天云趋势在2012年下半年开始为某大型国有银行的历史交易数据备份及查询提供基于Hadoop的技术解决方案,由于行业的特殊性,客户对服务的可 用性有着非常高的要求,而HDFS长久以来都被单点故 ...
- dojo 官方翻译 dojo/aspect
官网地址:http://dojotoolkit.org/reference-guide/1.10/dojo/aspect.html after() 定义:after(target, methodNam ...
- 20145109 《Java程序设计》第四周学习总结
20145109 <Java程序设计>第四周学习总结 教材学习内容总结 Chapter 6 Inheritance & Polymorphism What is Inheritan ...
- 利用MacBookPro入侵无线网络
目前无线网络的加密方式主要有WEP,WPA/WPA2.这是最常看到的加密方式,最近由于需要,专门去研究了一下如何入侵无线网络. 1.入侵WEP加密的无线网络 WEP加密方式现在已经很不安全了,因为只要 ...
- Shiro缓存使用Redis、Ehcache、自带的MpCache实现的三种方式实例
第一种:使用Redis做缓存,将数据存储到redis数据库中 第一步:在项目里面引入redis,配置文件如下: 配置文件:spring_shiro_redis.xml <?xml version ...
- VLAN虚拟局域网技术(一)-计算机网络
本文主要知识来源于学校课程,部分知识来自于H3C公司教材,未经许可,禁止转载.如需转载,请联系作者并注明出处. 1. VLAN(Virtual LAN):我们称之为虚拟局域网,它的作用就是将物理上互 ...
- NQueens, NQueens2 N皇后问题,递归回溯
N皇后的规则:任意两个皇后不在同一行,不在同一列,不在同一斜线上. 算法分析:这种问题就用回溯法.深度搜索然后回溯.用一个数组记录每一行皇后的位置,下标代表行,值代表列.对行深度搜索. public ...