Replies: 1 comment 1 reply
-
class Solution {
Map<TreeNode, Integer> mem = new HashMap<>();
public int rob(TreeNode root) {
if(root==null) return 0;
if(mem.containsKey(root)) return mem.get(root);
int doVal = root.val + (root.left == null ? 0 : rob(root.left.left) + rob(root.left.right))
+ (root.right == null ? 0 : rob(root.right.left) + rob(root.right.right));
int notDoVal = rob(root.left) + rob(root.right);
int res = Math.max(doVal, notDoVal);
mem.put(root, res);
return res;
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
taotie6
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
337. 打家劫舍 III
Beta Was this translation helpful? Give feedback.
All reactions