【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 ...
随机推荐
- 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...
- c#后台替换html标签的方法
public static string ReplaceHtmlTag(string html) { string strText = System.Tex ...
- 【PHP面向对象(OOP)编程入门教程】11.类的继承
继承作为面向对象的三个重要特性的一个方面,在面向对象的领域有着及其重要的作用,好像没听说哪个面向对象的语言不支持继承. 继承是PHP5面象对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个或 ...
- js字符串与16进制互相转换
// \x65\x76\x61\x6c是否启用\x加密 <script type="text/javascript"> function JavaDe() { var ...
- 关于CSS中对IE条件注释的问题
一.通用区分方式:IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别* ...
- UNITY3D在线更新之道-CSlight 使用总结
转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...
- hough变换检测线和圆
参考:http://blog.163.com/yuyang_tech/blog/static/21605008320130233343990/ 这篇介绍的基本思想. http://www.cnblog ...
- vs2012+qt5.2.0环境搭建
1.安装vs2012: 2.下载Qt 5.2.0 for Windows 32-bit(VS 2012, 579 MB) 和 Visual Studio Add-in 1.2.2for Qt5 注意: ...
- js ajax简单使用
文章部分资源来源:(http://blog.csdn.net/lzkkevin/article/details/6777474)以及(http://www.cnblogs.com/hwx0807/ar ...
- (转)Singleton 单例模式(懒汉方式和饿汉方式)
原文地址:http://www.cnblogs.com/kkgreen/archive/2011/09/05/2166868.html 单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某 ...