# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up