Medium
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root
.
Besides the root
, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root
of the binary tree, return the maximum amount of money the thief can rob without alerting the police.
Example 1:
Input: root = [3,2,3,null,3,null,1]
Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
Input: root = [3,4,5,1,3,null,1]
Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
Constraints:
[1, 104]
.0 <= Node.val <= 104
import com_github_leetcode.TreeNode
/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
fun rob(root: TreeNode?): Int {
val out = robRec(root)
return Math.max(out[0], out[1])
}
private fun robRec(curr: TreeNode?): IntArray {
if (curr == null) {
return intArrayOf(0, 0)
}
val left = robRec(curr.left)
val right = robRec(curr.right)
val out = IntArray(2)
// 1. If choosing to take the house
out[0] = curr.`val` + left[1] + right[1]
// 2. If choosing to skip the house
out[1] = left[0] + right[0]
// 3. Best Solution at house
out[0] = Math.max(out[0], out[1])
return out
}
}