Limited time: AI code review, hints, mock interviews, whiteboard analysis, and all Pro features are unlocked. Enroll

Binary Tree Cameras

Hard

Topics
TreeDFSDynamic ProgrammingGreedy

You are given the root of a binary tree. A camera placed on a node can monitor its parent, itself, and its immediate children. Return the minimum number of cameras needed to monitor every node of the tree.

Example 1

Input:  root = [0,0,null,0,0]
Output: 1

Example 2

Input:  root = [0,0,null,0,null,0,null,null,0]
Output: 2

Constraints

  • 1 <= number of nodes <= 1000
  • Node.val == 0.
Run ⌘' · Submit ⌘⏎
SensAI SensAI
Beta
Listening...
Tap mic to stop voice mode

Unlock AI Features

One click to activate - no payment, no credit card. Just sign in and you're in.

AI code review and hints
SensAI chat assistant
AI mock interviews
Whiteboard analysis
100% free during early access