---
# System prepended metadata

title: 0337. House Robber III
tags: [Leetcode, Recursion, Medium]

---

# 0337. House Robber III
###### tags: `Leetcode` `Medium` `Recursion`
Link: https://leetcode.com/problems/house-robber-iii/description/
## 思路
## Code
```java=
class Solution {
    public int rob(TreeNode root) {
        int[] res = robSub(root);
        return Math.max(res[0], res[1]);
    }
    private int[] robSub(TreeNode root) {
        int[] res = new int[2];
        if(root==null) return res;
        //res[0] stole currNode
        //res[1] not stole currNode
        int[] left = robSub(root.left);
        int[] right = robSub(root.right);
        res[0] = root.val+left[1]+right[1];
        res[1] = Math.max(left[0], left[1])+Math.max(right[0], right[1]);
        return res;
    }
}
```