Skip to content

Commit 7486c2b

Browse files
committed
[Tree] Add a solution to House Robber III
1 parent dcc3eaa commit 7486c2b

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 246 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 247 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -162,8 +162,9 @@
162162
[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| [Swift](./Tree/ConstructBinaryTreeInorderPostorder.swift)| Medium| O(n)| O(n)|
163163
[Path Sum](https://leetcode.com/problems/path-sum/)| [Swift](./Tree/PathSum.swift)| Easy| O(n)| O(n)|
164164
[Path Sum II](https://leetcode.com/problems/path-sum-ii/)| [Swift](./Tree/PathSumII.swift)| Medium| O(n)| O(n)|
165-
[Path Sum III](https://leetcode.com/problems/path-sum-iiI/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
165+
[Path Sum III](https://leetcode.com/problems/path-sum-iii/)| [Swift](./Tree/PathSumIII.swift)| Easy| O(n^2)| O(1)|
166166
[Bnary Tree Paths](https://leetcode.com/problems/binary-tree-paths/)| [Swift](./Tree/BnaryTreePaths.swift)| Easy| O(n)| O(n)|
167+
[House Robber III](https://leetcode.com/problems/house-robber-iii/)| [Swift](./Tree/HouseRobberIII.swift)| Medium| O(n)| O(1)|
167168
[Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)| [Swift](./Tree/UniqueBinarySearchTrees.swift)| Medium| O(n^2)| O(n)|
168169
[Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)| [Swift](./Tree/RecoverBinarySearchTree.swift)| Hard| O(n)| O(1)|
169170
[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Swift](./Tree/MergeTwoBinaryTrees.swift) | Easy | O(n) | O(n) |
@@ -484,7 +485,7 @@
484485
| [Swift](./String/LongestSubstringMostKDistinctCharacters.swift) | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | Hard
485486
| [Swift](./DP/NestedListWeightSum.swift) | 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | Easy
486487
| [Swift](./Math/CountingBits.swift) | 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | Medium
487-
| | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | Medium
488+
| [Swift](./Tree/HouseRobberIII.swift) | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | Medium
488489
| | 336 | [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | Hard
489490
| | 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | Hard
490491
| | 334 | [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | Medium

Tree/HouseRobberIII.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/house-robber-iii/
3+
* Primary idea: Using two sums to track rob sum starting from current node or not,
4+
* compare and get the maximum one
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 HouseRobberIII {
21+
func rob(_ root: TreeNode?) -> Int {
22+
let (robRoot, notRobRoot) = helper(root)
23+
24+
return max(robRoot, notRobRoot)
25+
}
26+
27+
fileprivate func helper(_ node: TreeNode?) -> (Int, Int) {
28+
guard let node = node else {
29+
return (0, 0)
30+
}
31+
32+
let (robLeft, notRobLeft) = helper(node.left)
33+
let (robRight, notRobRight) = helper(node.right)
34+
35+
let robNode = notRobLeft + notRobRight + node.val
36+
let notRobNode = max(robLeft, notRobLeft) + max(robRight, notRobRight)
37+
38+
return (robNode, notRobNode)
39+
}
40+
}

0 commit comments

Comments
 (0)