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 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.
分析
如果这个作为root,那么最长路应该就是..
F(left) + F(right) + val...当然如果left,或者right<0就不用加了的= =
从下往上递归遍历...
如果不这个不是root,那么就不能把left和right加起来了...因为只是一条路...
AC代码
/**
* 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 maxVal = INT_MIN;
int maxPathSum(TreeNode* root) {
if (root == NULL)
return ; maxSum(root);
return maxVal;
} /*递归函数*/
int maxSum(TreeNode *root)
{
if (root == NULL)
return ; /*求以root为根的当前子树的最大路径和*/
int curVal = root->val;
int lmaxSum = maxSum(root->left), rmaxSum = maxSum(root->right);
if (lmaxSum > )
curVal += lmaxSum;
if (rmaxSum > )
curVal += rmaxSum; if (curVal > maxVal)
maxVal = curVal; /*返回以当前root为根的子树的最大路径和*/
return max(root->val, max(root->val + lmaxSum, root->val + rmaxSum));
}
};
LeetCode(124) Binary Tree Maximum Path Sum的更多相关文章
- (算法)Binary Tree Max Path Sum
题目: Given a binary tree, find the maximum path sum. For this problem, a path is defined as any seque ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 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
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(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- 【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 ...
随机推荐
- gulp配置文件备份
/** * Created by leyi on 2016/8/25 0025. */ /*********************package.json依赖模块****************** ...
- javascript判断对象
var assert = require('chai').assert; describe('Array', function() { describe('#indexOf()', function( ...
- WPF的"路径标记语法"
在此之前我们先了解一下WPF的"路径标记语法" M:表示绘制起点 // M 0,0 L:表示绘制直线 (H:横线 V:竖线) // L 100,0 C:三次方贝塞尔曲线 // ...
- window.print() 去掉页眉页脚及打印链接【转载】
页面中添加样式: <style media="print"> @page { size: auto; /* auto is the initial value */ m ...
- 利用qmake生成Makefile文件
在linux下写程序,免不了要写Makefile文件,用automake,总感觉比较麻烦,linux人喜欢做麻烦的事,以显得风格迥异. 其实用qmake生成Makefile文件相当简单. 1 装好qm ...
- html window.open 使用详解
window.open ('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no ...
- secure boot(安全启动)下为内核模块签名
上一篇随笔中提到了如何在secure boot下安装Nvidia显卡驱动 >>上一篇随笔 如果不需要安装Nvidia显卡驱动,而且要生成密钥,可以参考>> 这篇文章 这里假设生 ...
- 手机版 div拖动
<!doctype html> <html> <head> <title></title> <script type="te ...
- C语言学习笔记(二)_system系统调用及posix说明
1.9 System系统调用 System库函数的功能是执行操作系统的命令或者运行指定的程序.system库函数的调用需要#include<stdlib.h>这个头文件. #include ...
- DOCTYPE的重要性
<!DOCTYPE>是文档类型声明: 声明必须是 HTML 文档的第一行,位于 <html> 标签之前.明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTM ...