Skip to content

Commit a54b471

Browse files
committed
[Tree] Add a solution to Binary Tree Maximum Path Sum
1 parent 5237785 commit a54b471

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 299 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 300 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -199,6 +199,7 @@
199199
[Path Sum II](https://leetcode.com/problems/path-sum-ii/)| [Swift](./Tree/PathSumII.swift)| Medium| O(n)| O(n)|
200200
[Path Sum III](https://leetcode.com/problems/path-sum-iii/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
201201
[Bnary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Swift](./Tree/BnaryTreePaths.swift)| Easy| O(n)| O(n)|
202+
[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)| [Swift](./Tree/BinaryTreeMaximumPathSum.swift)| Hard| O(n)| O(1)|
202203
[House Robber III](https://leetcode.com/problems/house-robber-iii/)| [Swift](./Tree/HouseRobberIII.swift)| Medium| O(n)| O(1)|
203204
[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| [Swift](./Tree/UniqueBinarySearchTrees.swift)| Medium| O(n^2)| O(n)|
204205
[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| [Swift](./Tree/RecoverBinarySearchTree.swift)| Hard| O(n)| O(1)|
@@ -754,7 +755,7 @@
754755
| [Swift](./BFS/WordLadder.swift) | 127 | [Word Ladder](https://oj.leetcode.com/problems/word-ladder/) | Medium |
755756
| | 126 | [Word Ladder II](https://oj.leetcode.com/problems/word-ladder-ii/) | Hard |
756757
| [Swift](./String/ValidPalindrome.swift) | 125 | [Valid Palindrome](https://oj.leetcode.com/problems/valid-palindrome/) | Easy |
757-
| | 124 | [Binary Tree Maximum Path Sum](https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/) | Hard |
758+
| [Swift](./Tree/BinaryTreeMaximumPathSum.swift) | 124 | [Binary Tree Maximum Path Sum](https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/) | Hard |
758759
| [Swift](./DP/BestTimeBuySellStockIII.swift) | 123 | [Best Time to Buy and Sell Stock III](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) | Hard |
759760
| [Swift](./DP/BestTimeBuySellStockII.swift) | 122 | [Best Time to Buy and Sell Stock II](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | Medium |
760761
| [Swift](./DP/BestTimeBuySellStock.swift) | 121 | [Best Time to Buy and Sell Stock](https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/) | Easy |

Tree/BinaryTreeMaximumPathSum.swift

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/binary-tree-maximum-path-sum/
3+
* Primary idea: Iterate all tree nodes and calculate each node's maximum weight value
4+
* and update maxmum path sum along the iteration
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
* Definition for a binary tree node.
8+
* public class TreeNode {
9+
* public var val: Int
10+
* public var left: TreeNode?
11+
* public var right: TreeNode?
12+
* public init(_ val: Int) {
13+
* self.val = val
14+
* self.left = nil
15+
* self.right = nil
16+
* }
17+
* }
18+
*/
19+
20+
class BinaryTreeMaximumPathSum {
21+
22+
var maxValue = Int.min
23+
24+
func maxPathSum(_ root: TreeNode?) -> Int {
25+
maxWeight(root)
26+
return maxValue
27+
}
28+
29+
private func maxWeight(_ node: TreeNode?) -> Int {
30+
guard let node = node else {
31+
return 0
32+
}
33+
34+
let leftMaxWeight = max(maxWeight(node.left), 0)
35+
let rightMaxWeight = max(maxWeight(node.right), 0)
36+
37+
maxValue = max(maxValue, leftMaxWeight + node.val + rightMaxWeight)
38+
39+
return node.val + max(leftMaxWeight, rightMaxWeight)
40+
}
41+
}

0 commit comments

Comments
 (0)