题目
思路
- 思路一:暴力递归,对于每个节点来说,如果我们选择该节点,就不能选择其子节点,如果不选择该节点就能选择其子节点,所以只要选取这几种情况的最大值即可
- 思路二:备忘录
思路三:动态规划,对于一棵树来说,选择就如暴力递归一样有两种选择,因此定义int[] res = new int[2],res[0]表示不选取该节点能够获得的最大值,res[1]节点表示选取该节点能够获取的最大值,然后使用后续遍历节点,最后的到res结果,我们再去其最大值即可
代码
//1.暴力递归public int rob(TreeNode root) {if (root == null) return 0;return Math.max((root.left != null ? rob(root.left.left) + rob(root.left.right) : 0) +(root.right != null ? rob(root.right.left) + rob(root.right.right) : 0) + root.val,rob(root.left) + rob(root.right));}//2.备忘录Map<TreeNode, Integer> map = new HashMap<>();public int rob(TreeNode root) {if (root == null) return 0;if (map.containsKey(root)) return map.get(root);int res = Math.max((root.left != null ? rob(root.left.left) + rob(root.left.right) : 0) +(root.right != null ? rob(root.right.left) + rob(root.right.right) : 0) + root.val,rob(root.left) + rob(root.right));map.put(root, res);return res;}//3.动态规划public int rob(TreeNode root) {if (root == null) return 0;int[] res = recur(root);return Math.max(res[0], res[1]);}//res[0]表示不抢劫当前节点,res[1]表示抢劫当前节点public int[] recur(TreeNode root) {if (root == null) return new int[2];int[] res = new int[2];int[] left = recur(root.left);int[] right = recur(root.right);res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);res[1] = root.val + left[0] + right[0];return res;}
