Given a binary tree, find the maximum path sum.

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

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

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

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

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. //Definition for binary tree
  9. struct TreeNode {
  10. int val;
  11. TreeNode *left;
  12. TreeNode *right;
  13. TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  14. };
  15. class Solution {
  16. public:
  17. int maxPathSum(TreeNode *root){
  18. if(root == NULL)
  19. {
  20. return ;
  21. }
  22. int MaxPathSum = root->val; //赋的初值一定要小于等于最后的值
  23. maxPathSumCur(root, MaxPathSum);
  24. return MaxPathSum;
  25. }
  26. int maxPathSumCur(TreeNode *root, int& MaxPathSum) {
  27. if(root == NULL)
  28. {
  29. return ;
  30. }
  31.  
  32. int lsum = maxPathSumCur(root->left, MaxPathSum);
  33. int rsum = maxPathSumCur(root->right, MaxPathSum);
  34. int maxPathSumCurrent = root->val; //每次根的值一定要加上 左右子树的就加大于0的
  35. if(lsum > )
  36. {
  37. maxPathSumCurrent += lsum;
  38. }
  39. if(rsum > )
  40. {
  41. maxPathSumCurrent += rsum;
  42. }
  43.  
  44. MaxPathSum = max(maxPathSumCurrent, MaxPathSum);
  45. return max(root->val, max(root->val + lsum, root->val +rsum)); //返回时返回根 节点加左 或右子树 或单独根节点中最大的
  46. }
  47. void create(TreeNode *& root)
  48. {
  49. int d;
  50. scanf("%d", &d);
  51. if(d != )
  52. {
  53. root = new TreeNode(d);
  54. create(root->left);
  55. create(root->right);
  56. }
  57. }
  58. };
  59.  
  60. int main()
  61. {
  62. Solution s;
  63. TreeNode * T = NULL;
  64. s.create(T);
  65. int sum = s.maxPathSum(T);
  66.  
  67. return ;
  68. }

【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. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...

  2. c#后台替换html标签的方法

     public static string ReplaceHtmlTag(string html)         {              string strText = System.Tex ...

  3. 【PHP面向对象(OOP)编程入门教程】11.类的继承

    继承作为面向对象的三个重要特性的一个方面,在面向对象的领域有着及其重要的作用,好像没听说哪个面向对象的语言不支持继承. 继承是PHP5面象对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个或 ...

  4. js字符串与16进制互相转换

    // \x65\x76\x61\x6c是否启用\x加密 <script type="text/javascript"> function JavaDe() { var ...

  5. 关于CSS中对IE条件注释的问题

    一.通用区分方式:IE6.IE7能识别*,标准浏览器(如FF)不能识别*:IE6能识别*,但不能识别 !important:IE7能识别*,也能识别 !important:IE8能识别\0,不能识别* ...

  6. UNITY3D在线更新之道-CSlight 使用总结

    转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...

  7. hough变换检测线和圆

    参考:http://blog.163.com/yuyang_tech/blog/static/21605008320130233343990/ 这篇介绍的基本思想. http://www.cnblog ...

  8. 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 注意: ...

  9. js ajax简单使用

    文章部分资源来源:(http://blog.csdn.net/lzkkevin/article/details/6777474)以及(http://www.cnblogs.com/hwx0807/ar ...

  10. (转)Singleton 单例模式(懒汉方式和饿汉方式)

    原文地址:http://www.cnblogs.com/kkgreen/archive/2011/09/05/2166868.html 单例模式的概念: 单例模式的意思就是只有一个实例.单例模式确保某 ...