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)的更多相关文章

  1. 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 ...

  2. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  3. 【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 ...

  4. 【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 ...

  5. 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 ...

  6. [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 ...

  7. 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 ...

  8. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  9. 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 ...

随机推荐

  1. php、打印

    <!DOCTYPE HTML><html><head><meta http-equiv="content-type" content=&q ...

  2. GCC用法

    http://www.cnblogs.com/hibernate6/archive/2010/08/24/2522057.html

  3. VirtulBox安装虚拟机(鼠标点击时)0x00000000指令引用的0x00000000内存该内存不能为written错误解决方案

    这个错误并不是所有人都会用到,我用的是WIN7系统,公司的电脑.查找了很多原因后,发现的确是由于系统主题被破解过的原因. 手工恢复风险太高.通过下面的工具就可以直接恢复.UniversalThemeP ...

  4. ecmall挂件开发实例二(转)

    下述例子讲述了快速增加挂件的方法,但对系统中的代码未做更改,基本参照image_ad挂件的方法. 第 一步: 了解首页模板结构 首页文 件结构 include hearder.html 包含的头文件, ...

  5. Kafka之sync、async以及oneway

    kafka有同步(sync).异步(async)以及oneway这三种发送方式,某些概念上区分也可以分为同步和异步两种,同步和异步的发送方式通过“producer.type”参数指定,而oneway由 ...

  6. 在ubuntu下,进行php7源码安装

    作为一名php的攻城师,如果没有玩php源码安装是说不过去的.我们知道php之所以这么流行,跟它的开源文化和lamp配套有很大关系.由于PHP7废弃了很多功能,所以一些依赖这些功能的程序可能无法运行, ...

  7. java web 程序---猜数字游戏

    思路:1.第一个是随机产生的数字,告诉我们去猜  cai.jsp 2.第二个是一个form表单,提交按钮后,将连接到验证页面 test1.jsp 3.第三个是比较猜的数和随机数.对了,提示再玩一次,不 ...

  8. Tool:Visual Studio

    ylbtech-Tool:Visual Studio Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.VS是一个基本完整的开发工具集,它包括了整个软件生命 ...

  9. Python GUI编程(Tkinter) windows界面开发

    Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...

  10. lnmp环境自动化部署

    lnmp.sh #!/bin/bash#This project to install lnmp#Author:菜逼cd命令玩家#Time:2016.10.13#objective:简化人工手动操作, ...