-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path124.二叉树中的最大路径和.py
68 lines (64 loc) · 1.4 KB
/
124.二叉树中的最大路径和.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#
# @lc app=leetcode.cn id=124 lang=python3
#
# [124] 二叉树中的最大路径和
#
# https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/description/
#
# algorithms
# Hard (39.26%)
# Likes: 340
# Dislikes: 0
# Total Accepted: 27.8K
# Total Submissions: 69.8K
# Testcase Example: '[1,2,3]'
#
# 给定一个非空二叉树,返回其最大路径和。
#
# 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列。该路径至少包含一个节点,且不一定经过根节点。
#
# 示例 1:
#
# 输入: [1,2,3]
#
# 1
# / \
# 2 3
#
# 输出: 6
#
#
# 示例 2:
#
# 输入: [-10,9,20,null,null,15,7]
#
# -10
# / \
# 9 20
# / \
# 15 7
#
# 输出: 42
#
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxPathSum(self, root: TreeNode) -> int:
self.max_sum = float('-inf')
def maxGain(node):
if not node:
return 0
left = max(maxGain(node.left), 0)
right = max(maxGain(node.right), 0)
cur_sum = node.val + left + right
self.max_sum = max(self.max_sum, cur_sum)
return node.val + max(left, right)
maxGain(root)
return self.max_sum
# @lc code=end