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. nyoj 4 779 兰州烧饼

    兰州烧饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 烧饼有两面,要做好一个兰州烧饼,要两面都弄热.当然,一次只能弄一个的话,效率就太低了.有这么一个大平底锅,一 ...

  2. WPF:类型转换器的实现

    类型转换器提供字符串文本到值的转换方法来帮助WPF设计时在XAML中配置属性.具体用法可以参考MSDN的文档:如何:实现类型转换器. 下面是一个Demo,参考自<葵花宝典--WPF自学手册> ...

  3. 【转】WordPress转PHPCMS策略-数据库完美转换

    来源:http://www.sjyhome.com/php/wp-to-pc-sql.html WordPress的访问速度不可恭维?那就试试能够生成纯静态的PHPCMS,保证能够让你的网页访问速度有 ...

  4. leetcode 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. Opencv二值图像的分布直方图

    Mat img; ]; int main() { VideoCapture video(); if (!video.isOpened()) { ; } Mat img; Mat img1, img2, ...

  6. OSI参考模型与TCP/IP协议模型

    OSI和TCP/IP都是为了计算机之间更好的互联的. 计算机网络是一个复杂的系统,比如两台计算机进行通信不仅仅只是有一条通信线就可以了. 还有很多的工作需要完成,例如: 如何知道对方计算机是否做好准备 ...

  7. 数据结构大二课程设计:QT实现线段树

    源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...

  8. python中单元测试/数据库预处理的技巧

    假设文件结构: pkg/ __init__.py components/ core.py __init__.py tests/ core_test.py __init__.py python -m 你 ...

  9. php返回json数组

    1.后端 //处理json数组中文问题 function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { s ...

  10. http statusCode(状态码)

    转自:1) http://specs.openstack.org/openstack/keystone-specs/api/v3/identity-api-v3.html#unauthorized   ...