House Robber III LeetCode Solution

Last updated on July 19th, 2024 at 09:54 pm

Here, We see House Robber III LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Depth-First Search, Tree

Companies

Uber

Level of Question

Medium

House Robber III LeetCode Solution

House Robber III LeetCode Solution

Problem Statement

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called 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 form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.

Example 1:

rob1 tree

Input: root = [3,2,3,null,3,null,1]
Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

rob2 tree

Input: root = [3,4,5,1,3,null,1]
Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.

1. House Robber III LeetCode Solution C++

class Solution {
public:
    int rob(TreeNode* root) {
        if (!root) return 0;
        vector<int> houseRows;
        int len = 1, dp[3];
        TreeNode *curr;
        queue<TreeNode*> q;
        q.push(root);
        while (len) {
            houseRows.push_back(0);
            while (len--) {
                curr = q.front();
                q.pop();
                houseRows.back() += curr->val;
                if (curr->left) q.push(curr->left);
                if (curr->right) q.push(curr->right);
            }
            len = q.size();
        }
        len = houseRows.size();
        dp[0] = houseRows[0];
        dp[1] = max(houseRows[0], houseRows[1]);
        dp[2] = max(houseRows[2] + dp[0], dp[1]);
        for (int n: dp) cout << n << ' ';
        for (int i = 3; i < len; i++) {
            dp[i % 3] = max(dp[(i - 1) % 3], max(dp[(i - 3) % 3], dp[(i - 2) % 3]) + houseRows[i]);
        }
        return dp[--len % 3];
    }
};

2. House Robber III Solution Java

public class Solution {
    public int rob(TreeNode root) {
        int[] num = dfs(root);
        return Math.max(num[0], num[1]);
    }
    private int[] dfs(TreeNode x) {
        if (x == null) return new int[2];
        int[] left = dfs(x.left);
        int[] right = dfs(x.right);
        int[] res = new int[2];
        res[0] = left[1] + right[1] + x.val;
        res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        return res;
    }
}

3. House Robber III Solution JavaScript

var rob = function(root) {
    function helper(node){
        if(!node) return [0,0];
        const [lr,ln] = helper(node.left);
        const [rr, rn] = helper(node.right);
        return [node.val + ln + rn, Math.max(lr+rr, ln+rn, lr+rn, ln+rr)];
    }
    return Math.max(...helper(root));
};

4. House Robber III Solution Python

class Solution(object):
    def rob(self, root):
        return max(self.dfs(root))
    def dfs(self, root):
        if not root:
            return (0, 0)
        left = self.dfs(root.left)
        right = self.dfs(root.right)
        return (root.val + left[1] + right[1], max(left[0], left[1]) + max(right[0], right[1]))
Scroll to Top