House Robber III

A thief is attempting to rob houses arranged in a binary tree structure. Each house is a node in the tree, and two directly-linked houses cannot be robbed in the same night. The goal is to determine the maximum amount of money the thief can rob without alerting the police, starting from the root of the tree.

Constraints

  • The number of nodes in the tree is in the range [1, 10^4].
  • 0 <= Node.val <= 10^4
Saving… · 11 lines
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right

class Solution:
def rob(self, root: TreeNode) -> int:


Console

Click Run to execute sample tests.