原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/

题意:

Given a binary tree, find the maximum path sum.

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

For example:
Given the below binary tree,

       1
/ \
2 3

Return 6.

解题思路:这道题是在树中寻找一条路径,这条路径上的节点的和为最大,起点和终点只要是树里面的节点就可以了。这里需要注意的一点是:节点值有可能为负值。解决这道二叉树的题目还是来使用递归。例如下面这棵树:

            1

             /     \

           2        3

        /    \    /    \

         4     5  6     7

对于这棵树而言,和为最大的路径为:5->2->1->3->7。

那么这条路径是怎么求出来的呢?这里需要用到一个全局变量Solution.max,可以随时被更大的路径和替换掉。在函数递归到左子树时:最大的路径为:4->2->5。但此时函数的返回值应当为4->2和5->2这两条路径中和最大的一条。右子树同理。而Solution.max用来监控每个子树中的最大路径和。那么思路就是:(左子树中的最大路径和,右子树中的最大路径和,以及左子树中以root.left为起点的最大路径(需要大于零)+右子树中以root.right为起点的最大路径(需要大于零)+root.val),这三者中的最大值就是最大的路径和。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param root, a tree node
# @return an integer
def maxsum(self, root):
if root == None: return 0
sum = root.val
lmax = 0; rmax = 0
if root.left:
lmax = self.maxsum(root.left)
if lmax > 0:
sum += lmax
if root.right:
rmax = self.maxsum(root.right)
if rmax > 0:
sum += rmax
if sum > Solution.max: Solution.max = sum
return max(root.val, max(root.val + lmax, root.val + rmax)) def maxPathSum(self, root):
Solution.max = -10000000
if root == None: return 0
self.maxsum(root)
return Solution.max

[leetcode]Binary Tree Maximum Path Sum @ Python的更多相关文章

  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: Binary Tree Maximum Path Sum 解题报告

    Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...

  3. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  4. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  5. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  6. C++ leetcode Binary Tree Maximum Path Sum

    偶然在面试题里面看到这个题所以就在Leetcode上找了一下,不过Leetcode上的比较简单一点. 题目: Given a binary tree, find the maximum path su ...

  7. [LeetCode] Binary Tree Maximum Path Sum(最大路径和)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  8. [Leetcode] Binary tree maximum path sum求二叉树最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  9. LeetCode Binary Tree Maximum Path Sum 二叉树最大路径和(DFS)

    题意:给一棵二叉树,要求找出任意两个节点(也可以只是一个点)的最大路径和,至少1个节点,返回路径和.(点权有负的.) 思路:DFS解决,返回值是,经过从某后代节点上来到当前节点且路径和最大的值.要注意 ...

随机推荐

  1. Spark入门2(Spark简析)

    一.Spark核心概念-RDD RDD是弹性分布式数据集,一个RDD由多个partition构成,一个partition对应一个task.RDD的操作分为两种:Trasformation(把一个RDD ...

  2. [leetcode sort]147. Insertion Sort List

    Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...

  3. Linux命令学习<不断更新>

    没有系统的学习过Linux命令,遇到了就学习一下,慢慢积累. 1.echo 命令,学习网站『https://linux.cn/article-3948-1.html』. echo单词有回声.共鸣的意思 ...

  4. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  5. C#高级编程9-第10章 集合

    集合 1.集合接口和类型 接口 说明 IEnumerable<T> 如果foreach语句用于集合,就需要IEnumerable接口.这个借口定义了方法GetEnumerator(),他返 ...

  6. 通过 ssh 登录到手机 Termux

    通过ssh登录到手机 Termux 测试环境 电脑: macOS Mojave 手机: Huawei Mate10Pro Termux是Android上的一个非常强大的终端模拟器. 强大之处在于支持使 ...

  7. js删除字符串的最后一个字符三种方法

    字符串 var basic = "abc,def,ghi,"; 第一种 basic = basic.substr(0, basic.length - 1); 第二种 basic = ...

  8. maven 之dependencyManagement 和 pluginManagement

    一个大中型Java项目中,一般由若干个module组成,各个module各司其职,担任整个工程中不同角色.大多数情况下,大多数module都会用到相同的jar包,或者插件.如果每个module中都引入 ...

  9. 记录一次软件Bug发生的过程

    结束一周的紧张工作,难得的休息时光,坐在电脑前浏览博客.听听歌.看看大片,这也算是一种享受. 因为年度的开发任务已经开始了,所以最近会特别忙,新人的成长又没有想象中的好,经常在他们身上看到自己去年的影 ...

  10. Nucleus PLUS任务调度

    概述 Nucleus Plus内核(Kernel)的主要目的是管理实时任务的竞争执行(共享CPU),为应用提供各种便利,高速响应外部事件.Nucleus Plus的系统结构如图1所看到的,能够看出线程 ...