124. Binary Tree Maximum Path Sum (Tree; DFS)
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
.
思路:存在val小于零的情况,所以path不一定是从叶子节点到叶子节点;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxPathSum(TreeNode* root) {
maxPath = INT_MIN;
postOrderTraverse(root);
return maxPath;
} int postOrderTraverse(TreeNode* root){
if(root==NULL) return ;
int left, right, sum;
left = postOrderTraverse(root->left);
right = postOrderTraverse(root->right);
left = max(left,); //ignore negative value
right = max(right,);
sum = left + right +root->val;
if(sum > maxPath) maxPath = sum;
return root->val+max(left,right);//return current maximum sum from one child branch to root
}
private:
int maxPath;
};
124. Binary Tree Maximum Path Sum (Tree; 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】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 ...
- 【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: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- [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 ...
- 26. 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] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- 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 ...
随机推荐
- c# Dictionary字典类如何使用
Dictionary<string, string> openWith = new Dictionary<string, string>(); //添加元素 openWith. ...
- 常用ETL工具
一 ETL工具[国外] 1. datastage点评:最专业的ETL工具,价格不菲,使用难度一般 下载地址:ftp://ftp.seu.edu.cn/Pub/Develop ... taStage.v ...
- 嵌入式视频采集编程思路(Video 4 Linux)-转
转自:http://zyg0227.blog.51cto.com/1043164/271954 1. linux 内核有video for linux简称V4L.V4L是Linux影像系统与嵌入式影 ...
- bzoj 3158 千钧一发——网络流
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3158 发现偶数之间一定满足第二个条件:奇数之间一定满足第一个条件 ( \( (2m+1)^{ ...
- angular-ui-bootstrap的弹出框定义成一个服务的实践(二)
定义一个弹出框的服务:alert_box defiine(["app"],function(mainapp){ mainapp.controller('ModalInstanceC ...
- Linux 利用busybox制作根文件系统
busybox版本:1.17.3 官网下载路径:https://busybox.net/downloads/ 网盘下载路径:https://pan.baidu.com/s/1nvrEa73 密码:7y ...
- SmtpClient发送邮件
使用第三方SMTP服务器来发送邮件.如网易: SmtpClient sc = new SmtpClient("smtp.126.com"); sc.Credentials = ne ...
- 项目中Map端内存占用的分析
最近在项目中开展重构活动,对Map端内存尽量要省一些,当前的系统中Map端内存最高占用大概3G左右(设置成2G时会导致Java Heap OOM).虽然个人觉得占用不算多,但是显然这样的结果想要试 ...
- Go - 常量与运算符
常量的定义 1. 常量的值在编译的时候就已经确定.所以,在定义的时候就必须赋值 2. 使用 const 关键字来声明常量.赋值形式与变量类似: // 标准定义 const PI int = 3.14 ...
- ALSA声卡12_从零编写之添加音量控制_学习笔记
1.设置音量时应用程序的调用过程 (1)strace分析: amixer cset numid=1 30 (设置音量) /dev/snd/controlC0 open SNDRV_CTL_IOCTL_ ...