【leetcode】Binary Tree Maximum Path Sum (medium)
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)的更多相关文章
- 【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 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 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- 解决pydev报unsolved import的问题
安装Flask_RESTful-0.2.11包后, 并在pydev 对应的 interpreter 重新刷新了System PYTHONPATH, 看见Lib\site-packages\flask_ ...
- ggplot2 上篇
title: "ggplot2 上篇" author: "li_volleyball" date: "2016年4月16日" output: ...
- VC++ 6.0使用定时器SetTimer;
背景: windows中使用VC++6.0制作了个交互界面向下位机定时发送数据及显示下位机上传的数据.定时发送则需要使用定时器. 本文只做记录如何调用,原理以后再深究. 正文: 首先,我生成的窗体类名 ...
- 【bzoj1864】[ZJOI2006]三色二叉树
题目描述 输入 仅有一行,不超过500000个字符,表示一个二叉树序列. 输出 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. 样例输入 1122002010 样例输出 ...
- r8 - ASC 41(俄罗斯多校)
1 今天干的俄罗斯的一场多校,被虐哭啊,就做出两题. 2 3 4 5 6 7 Gym 100496D Data Mining 8 题目讲得是给你一串数字,然后给你i,p,表示从第i开始,对这串数离散话 ...
- jQuery框架分析第一章: 第一个匿名函数
我的jQuery版本为1.7* 这个版本代码比之前的版本优化了很多,结构也清晰了不少,就用最新的吧. 打开jQuery源代码 首先你能看到所有代码被一个 (function(window,undefi ...
- post 之checkbox 未被选中解决方法
第一种方法: http://cnn237111.blog.51cto.com/2359144/1293812 第二种方法(推荐): http://blog.csdn.net/xyanghomepage ...
- JAVA操作ORACLE数据库的存储过程
一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...
- 如何安装最新的 XFCE 桌面?
导读 Xfce 是一款针对 Linux 系统的现代化轻型开源桌面环境,它在其他的类 Unix 系统上,比如 Mac OS X. Solaries. *BSD 以及其它几种上也能工作得很好.它非常快并以 ...
- explicit,violate,volatile,mutable小结
转自:http://blog.csdn.net/helonsy/article/details/7091130 explicit:放在构造函数前面可以阻止构造函数的隐式类型转换.这样可以避免不必要的错 ...