Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

找树的最大路径和 注意路径可以从任意点起始和结束。

我发现我真的还挺擅长树的题目的,递归不难。就是因为有个需要比较的量(最大和),所以需要再写一个函数。

因为路径可以从任意点起始和结束,所以每次递归的时候左右子树小于等于0的就可以不管了。

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; //Definition for binary tree
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int maxPathSum(TreeNode *root){
if(root == NULL)
{
return ;
}
int MaxPathSum = root->val; //赋的初值一定要小于等于最后的值
maxPathSumCur(root, MaxPathSum);
return MaxPathSum;
}
int maxPathSumCur(TreeNode *root, int& MaxPathSum) {
if(root == NULL)
{
return ;
} int lsum = maxPathSumCur(root->left, MaxPathSum);
int rsum = maxPathSumCur(root->right, MaxPathSum);
int maxPathSumCurrent = root->val; //每次根的值一定要加上 左右子树的就加大于0的
if(lsum > )
{
maxPathSumCurrent += lsum;
}
if(rsum > )
{
maxPathSumCurrent += rsum;
} MaxPathSum = max(maxPathSumCurrent, MaxPathSum);
return max(root->val, max(root->val + lsum, root->val +rsum)); //返回时返回根 节点加左 或右子树 或单独根节点中最大的
}
void create(TreeNode *& root)
{
int d;
scanf("%d", &d);
if(d != )
{
root = new TreeNode(d);
create(root->left);
create(root->right);
}
}
}; int main()
{
Solution s;
TreeNode * T = NULL;
s.create(T);
int sum = s.maxPathSum(T); return ;
}

【leetcode】Binary Tree Maximum Path Sum (medium)的更多相关文章

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

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

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

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

  4. 【LeetCode OJ】Binary Tree Maximum Path Sum

    Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...

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

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

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

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

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

随机推荐

  1. Unity3D 学习笔记

    不是什么技术文章,纯粹是我个人学习是遇到一些觉得需要注意的要点,当成笔记. 1.关于调试,在Android下无法断点,Debug也无法查看,查看日志方法可以启动adb的log功能,或者自己写个GUI控 ...

  2. 【AngularJS】—— 11 指令的交互

    前面基本了解了指令的相关内容: 1 如何自定义指令 2 指令的复用 本篇看一下指令之间如何交互.学习内容来自<慕课网 指令3> 背景介绍 这例子是视频中的例子,有一个动感超人,有三种能力, ...

  3. AngularJS API之extend扩展对象

    angular.extend(dst,src),在我实验的1.2.16版本上是支持深拷贝的.但是最新的API显示,这个方法是不支持深拷贝的. 另外,第二个参数src支持多个对象. 第一种使用方式 va ...

  4. CLGeocoder "Lost connection to geod" #error# when use geocodeAddressString:completionHandler

      I got this warning when I tried to get destination using CLGeoCoder and the warning is coming out ...

  5. ThinkPHP3.2.3 安装教程

    本文以  Windows  平台为例 安装前准备:Windows操作系统的电脑,php编程环境(配置好了Apache.MySql.php).推荐wampserver.               待安 ...

  6. HTMLDOM中三种元素节点、属性节点、文本节点的测试案例

    HTML dom中常用的三种节点分别是元素节点.属性节点.文本节点. 具体指的内容可参考下图: 以下为测试用例: <!DOCTYPE html> <html> <head ...

  7. Python命令 (if __name__=="__main__":)

    1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...

  8. 【PHP面向对象(OOP)编程入门教程】10.__set(),__get(),__isset(),__unset()四个方法的应用

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...

  9. 【C语言入门教程】4.4 指针 与 指针变量

    在程序中声明变量后,编译器就会为该变量分配相应的内存单元.也就是说,每个变量在内存会有固定的位置,有具体的地址.由于变量的数据类型不同,它所占的内存单元数也不相同.如下列声明了一些变量和数组. int ...

  10. js面试题

    前几天在学习js的时候,碰到了这样一道面试题,要求计算出给你一个随机乱敲的一个字符串,要求在其中找出那个字符出现的次数最多,以及出现的个数. 这你有两种方案,请大家仔细阅读,有可能在你将来的面试中会碰 ...