337. House Robber III java solutions

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3
    /    2   3
    \   \
     3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

     3
    /    4   5
  / \   \
 1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int rob(TreeNode root) {
12         int[] res = Robdfs(root);
13         return Math.max(res[0],res[1]);
14     }
15
16     public int[] Robdfs(TreeNode root){
17         if(root == null) return new int[2];
18         int[] left = Robdfs(root.left);
19         int[] right = Robdfs(root.right);
20         int[] ans = new int[2];
21         ans[0] = left[1] + root.val + right[1];
22         ans[1] = Math.max(left[0], left[1]) + Math.max(right[0],right[1]);
23         return ans;
24     }
25 }

DFS中,root为null时,返回长度为2的空数组;
建立结果数组res时,res[0]是包括根节点的情况,res[1]是不包含根节点的情况。而非按左右子树来进行划分的。

时间: 2024-08-01 17:04:03

337. House Robber III java solutions的相关文章

337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

[leetcode] 337.House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

<LeetCode OJ> 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent

213. House Robber II Java Solutions

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor o

<LeetCode OJ> 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent

216. Combination Sum III java solutions

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n

LeetCode in Python 337. House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all hou

LeetCode 337. House Robber III 动态演示

每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{cur, false}]表示以cur为根的树,不偷cur节点的钱,最多能偷的钱 可以看出有下面的关系 mp[{node, false}] = mp[{node->left,true}] + mp[{node->right,true}] mp[{node, true}] = max(node->