Skip to content

Commit cfa95da

Browse files
committed
feat: add 124
1 parent cb06c17 commit cfa95da

File tree

3 files changed

+105
-3
lines changed

3 files changed

+105
-3
lines changed

Hard/124 Binary Tree Maximum Path Sum.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,59 @@
22

33
## Intuition
44

5+
The maximum path sum in a binary tree can be found by considering all possible paths that pass through each node. For any given node, the maximum path sum could be:
6+
7+
1. The node's value itself
8+
2. The node's value plus the maximum path from its left subtree
9+
3. The node's value plus the maximum path from its right subtree
10+
4. The node's value plus both left and right maximum paths
11+
512
## Approach
613

14+
We use a post-order traversal approach to solve this problem:
15+
16+
1. For each node, we recursively calculate the maximum path sum from its left and right subtrees
17+
2. We update the global maximum by considering all possible combinations:
18+
- The current node's value alone
19+
- Current node + left path
20+
- Current node + right path
21+
- Current node + left path + right path
22+
3. We return the maximum path sum that can be extended from the current node to its parent (which can only include at most one of its children)
23+
724
## Complexity
825

9-
- Time complexity:
10-
- Space complexity:
26+
- Time complexity: O(n)
27+
- Space complexity: O(h)
1128

1229
## Keywords
1330

31+
- Binary Tree
32+
- Post-order Traversal
33+
- Recursion
34+
1435
## Code
1536

1637
```go
17-
38+
/**
39+
* Definition for a binary tree node.
40+
* type TreeNode struct {
41+
* Val int
42+
* Left *TreeNode
43+
* Right *TreeNode
44+
* }
45+
*/
46+
func maxPathSum(root *TreeNode) int {
47+
ret := math.MinInt
48+
var postOrder func(node *TreeNode) int
49+
postOrder = func(node *TreeNode) int {
50+
if node == nil {
51+
return 0
52+
}
53+
left, right := postOrder(node.Left), postOrder(node.Right)
54+
ret = max(ret, left + right + node.Val, left + node.Val, right + node.Val, node.Val)
55+
return max(left + node.Val, right + node.Val, node.Val)
56+
}
57+
ret = max(ret, postOrder(root))
58+
return ret
59+
}
1860
```

Week8/124 AC.png

112 KB
Loading
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 124. Binary Tree Maximum Path Sum
2+
3+
## Intuition
4+
5+
The maximum path sum in a binary tree can be found by considering all possible paths that pass through each node. For any given node, the maximum path sum could be:
6+
7+
1. The node's value itself
8+
2. The node's value plus the maximum path from its left subtree
9+
3. The node's value plus the maximum path from its right subtree
10+
4. The node's value plus both left and right maximum paths
11+
12+
## Approach
13+
14+
We use a post-order traversal approach to solve this problem:
15+
16+
1. For each node, we recursively calculate the maximum path sum from its left and right subtrees
17+
2. We update the global maximum by considering all possible combinations:
18+
- The current node's value alone
19+
- Current node + left path
20+
- Current node + right path
21+
- Current node + left path + right path
22+
3. We return the maximum path sum that can be extended from the current node to its parent (which can only include at most one of its children)
23+
24+
## Complexity
25+
26+
- Time complexity: O(n)
27+
- Space complexity: O(h)
28+
29+
## Keywords
30+
31+
- Binary Tree
32+
- Post-order Traversal
33+
- Recursion
34+
35+
## Code
36+
37+
```go
38+
/**
39+
* Definition for a binary tree node.
40+
* type TreeNode struct {
41+
* Val int
42+
* Left *TreeNode
43+
* Right *TreeNode
44+
* }
45+
*/
46+
func maxPathSum(root *TreeNode) int {
47+
ret := math.MinInt
48+
var postOrder func(node *TreeNode) int
49+
postOrder = func(node *TreeNode) int {
50+
if node == nil {
51+
return 0
52+
}
53+
left, right := postOrder(node.Left), postOrder(node.Right)
54+
ret = max(ret, left + right + node.Val, left + node.Val, right + node.Val, node.Val)
55+
return max(left + node.Val, right + node.Val, node.Val)
56+
}
57+
ret = max(ret, postOrder(root))
58+
return ret
59+
}
60+
```

0 commit comments

Comments
 (0)