LeetCode337] [動的計画:戦利品Ⅲ

package leetcode.editor.cn;

//在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“
//房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 
//
// 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 
//
// 示例 1: 
//
// 输入: [3,2,3,null,3,null,1]
//
//     3
//    / \
//   2   3
//    \   \ 
//     3   1
//
//输出: 7 
//解释: 小偷一晚能够盗取的最高金额 = 3 + 3 + 1 = 7. 
//
// 示例 2: 
//
// 输入: [3,4,5,1,3,null,1]
//
//     3
//    / \
//   4   5
//  / \   \ 
// 1   3   1
//
//输出: 9
//解释: 小偷一晚能够盗取的最高金额 = 4 + 5 = 9.
// 
// Related Topics 树 深度优先搜索


//Java:打家劫舍 III
public class P337打家劫舍III{
    public static void main(String[] args) {
        Solution solution = new P337打家劫舍III().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)

  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    class Solution {
        public int rob(TreeNode root) {
            if (root == null) {
                return 0;
            }
            return Math.max(robot(root, true), robot(root, false));
        }
        public int robot(TreeNode root, boolean isTake) {
            if (root == null) {
                return 0;
            }
            if (isTake) {
                return robot(root.left, false) + robot(root.right, false) + root.val;
            } else {
                int a1 = Math.max(robot(root.left, true), robot(root.left, false));
                int a2 = Math.max(robot(root.right, true), robot(root.right, false));
                return a1 + a2;
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}

 

公開された12元の記事 ウォンの賞賛0 ビュー10000 +

おすすめ

転載: blog.csdn.net/baidu_34209307/article/details/105108148