FastPrepHouse Robber III
Problems/House Robber III

House Robber III

Visa logoVisa● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In the second technical interview, along with questions about system concepts, the candidate got a coding question that they said was similar to House Robber III.

The report does not say how it differed. Here you get the standard version: the houses form a binary tree, you cannot take a house and its parent or child together, and you return the largest total. The tree size and depth limits are added for this practice version.

The judged task matches the report at about 70%.

The houses in a neighborhood are connected like a binary tree. Each node is a house, and its value is the amount of money in that house. root is the first house.

You may take money from any houses you like, but you can never take from two houses that are directly connected: a house and its parent, or a house and its child. Two houses with the same parent are not directly connected, so you may take both.

Return the largest total amount of money you can take.

Function

rob(root: TreeNode) → int

Examples

Example 1

root = [4,2,6,null,5,null,1]return = 11

House 4 is the root. Its children are 2 and 6. House 2 has child 5, and house 6 has child 1.

Take houses 5 and 6 for 5 + 6 = 11. They are on different levels, but they are not directly connected. Taking the root with 5 and 1 gives only 4 + 5 + 1 = 10.

Example 2

root = [1,10,1,null,null,10,10]return = 30

The root 1 has children 10 and 1, and the right child 1 has two children of 10.

Skip the root and its right child. Take the left child and the two bottom houses for 10 + 10 + 10 = 30.

Example 3

root = [2,1,null,1,null,2]return = 4

The houses form one line going down: 2, then 1, then 1, then 2.

Take the top and the bottom house for 2 + 2 = 4. Skipping two houses in a row in the middle is allowed.

Constraints

  • The number of nodes in the tree is between 1 and 10^4.
  • 0 <= Node.val <= 10^4
  • The depth of the tree is at most 500. The root alone has depth 1.

More Visa problems

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • House RobberPHONE SCREEN · Seen Nov 2026
All Visa interview questionsSee Visa hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        // Write your code here.
    }
}
Checking account…