Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 281d855

Browse files
committed
Merge pull request #372 from 0xff-dev/124
Add solution and test-cases for problem 124
2 parents de8b9f4 + 0d777d5 commit 281d855

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# [124.Binary Tree Maximum Path Sum][title]
2+
3+
## Description
4+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
5+
6+
The **path sum** of a path is the sum of the node's values in the path.
7+
8+
Given the `root` of a binary tree, return the maximum **path sum** of any **non-empty** path.
9+
10+
**Example 1:**
11+
![example1](./exx1.jpg)
12+
13+
```
14+
Input: root = [1,2,3]
15+
Output: 6
16+
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
17+
```
18+
19+
**Example 2:**
20+
![example2](./exx2.jpg)
21+
22+
```
23+
Input: root = [-10,9,20,null,null,15,7]
24+
Output: 42
25+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
26+
```
27+
28+
## 结语
29+
30+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
31+
32+
[title]: https://leetcode.com/problems/binary-tree-maximum-path-sum
33+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

‎leetcode/101-200/0124.Binary-Tree-Maximum-Path-Sum/Solution.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,54 @@ func max(x, y int) int {
2929
}
3030
return y
3131
}
32+
33+
func maxPathSum_2(root *TreeNode) int {
34+
ans := 0
35+
ansInit := false
36+
maxPathSum124(root, &ans, &ansInit)
37+
return ans
38+
}
39+
func maxPathSum124(root *TreeNode, ans *int, ansInit *bool) (int, int) {
40+
if root == nil {
41+
return 0, 0
42+
}
43+
44+
ll, lr := maxPathSum124(root.Left, ans, ansInit)
45+
rl, rr := maxPathSum124(root.Right, ans, ansInit)
46+
lmax := ll
47+
if lr > lmax {
48+
lmax = lr
49+
}
50+
51+
rmax := rr
52+
if rl > rmax {
53+
rmax = rl
54+
}
55+
56+
returnL, returnR := root.Val, root.Val
57+
l := lmax + root.Val
58+
r := rmax + root.Val
59+
if !*ansInit || l > *ans {
60+
*ans = l
61+
*ansInit = true
62+
}
63+
if !*ansInit || r > *ans {
64+
*ans = r
65+
*ansInit = true
66+
}
67+
if r := l + rmax; !*ansInit || r > *ans {
68+
*ans = r
69+
*ansInit = true
70+
}
71+
if !*ansInit || root.Val > *ans {
72+
*ans = root.Val
73+
*ansInit = true
74+
}
75+
if l > root.Val {
76+
returnL = l
77+
}
78+
if r > root.Val {
79+
returnR = r
80+
}
81+
return returnL, returnR
82+
}

‎leetcode/101-200/0124.Binary-Tree-Maximum-Path-Sum/Solution_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type SolutionFuncType func(node *TreeNode) int
1515

1616
var SolutionFuncList = []SolutionFuncType{
1717
maxPathSum_1,
18+
maxPathSum_2,
1819
}
1920

2021
// Test case info struct
6.83 KB
Loading[フレーム]
12.8 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /