337. House Robber III LeetCode Solution
In this guide, you will get 337. House Robber III LeetCode Solution with the best time and space complexity. The solution to House Robber III problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- House Robber III solution in C++
- House Robber III solution in Java
- House Robber III solution in Python
- Additional Resources

Problem Statement of House Robber III
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:
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:
Input: root = [3,4,5,1,3,null,1]
Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
Constraints:
The number of nodes in the tree is in the range [1, 104].
0 <= Node.val <= 104
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
337. House Robber III LeetCode Solution in C++
struct T {
int robRoot;
int notRobRoot;
};
class Solution {
public:
int rob(TreeNode* root) {
const auto& [robRoot, notRobRoot] = robOrNotRob(root);
return max(robRoot, notRobRoot);
}
private:
T robOrNotRob(TreeNode* root) {
if (root == nullptr)
return {0, 0};
const T l = robOrNotRob(root->left);
const T r = robOrNotRob(root->right);
return {root->val + l.notRobRoot + r.notRobRoot,
max(l.robRoot, l.notRobRoot) + max(r.robRoot, r.notRobRoot)};
}
};
/* code provided by PROGIEZ */
337. House Robber III LeetCode Solution in Java
class T {
public int robRoot;
public int notRobRoot;
public T(int robRoot, int notRobRoot) {
this.robRoot = robRoot;
this.notRobRoot = notRobRoot;
}
}
class Solution {
public int rob(TreeNode root) {
T t = robOrNotRob(root);
return Math.max(t.robRoot, t.notRobRoot);
}
private T robOrNotRob(TreeNode root) {
if (root == null)
return new T(0, 0);
T l = robOrNotRob(root.left);
T r = robOrNotRob(root.right);
return new T(root.val + l.notRobRoot + r.notRobRoot,
Math.max(l.robRoot, l.notRobRoot) + Math.max(r.robRoot, r.notRobRoot));
}
}
// code provided by PROGIEZ
337. House Robber III LeetCode Solution in Python
class Solution:
def rob(self, root: TreeNode | None) -> int:
def robOrNot(root: TreeNode | None) -> tuple:
if not root:
return (0, 0)
robLeft, notRobLeft = robOrNot(root.left)
robRight, notRobRight = robOrNot(root.right)
return (root.val + notRobLeft + notRobRight,
max(robLeft, notRobLeft) + max(robRight, notRobRight))
return max(robOrNot(root))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.