Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

968. Binary Tree Cameras

Hard46.9% Acceptance
Dynamic ProgrammingTreeDepth-First Search
Asked by:
Facebook
ProblemSolutions (7)VideosCompanies (8)Notes

Problem Statement

You are given the root of a binary tree. We install cameras on the tree nodes where each camera at a node can monitor its parent, itself, and its immediate children.

Return the minimum number of cameras needed to monitor all nodes of the tree.

Example 1:

Input: root = [0,0,null,0,0]
Output: 1
Explanation: One camera is enough to monitor all nodes if placed as shown.

Example 2:

Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are needed to monitor all nodes of the tree. The above image shows one of the valid configurations of camera placement.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • Node.val == 0
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
F
D
DE Shaw
A
Amazon
G
Google
A
Apple
+3

Approach

The key idea in Binary Tree Cameras is to place the minimum number of cameras so that every node in the binary tree is monitored. A camera at a node can monitor its parent, itself, and its immediate children. The most effective strategy uses a post-order Depth-First Search (DFS) combined with a small state-based dynamic programming idea.

During traversal, each node returns a state describing whether it has a camera, is covered, or is not covered. By processing children first, the algorithm decides if a camera must be placed at the current node (for example, when a child is not covered). This greedy decision works because information flows upward from the leaves to the root.

This DFS approach ensures cameras are placed only when necessary, leading to an optimal solution. Since each node is processed once, the algorithm runs in O(n) time with O(h) recursion space, where n is the number of nodes and h is the tree height.

Complexity

ApproachTime ComplexitySpace Complexity
DFS with Greedy State TrackingO(n)O(h)
Tree DP with Post-order TraversalO(n)O(h)

Video Solution Available

NeetCode

View all video solutions

Solutions (7)

Greedy Depth-First Search (DFS)

The greedy DFS solution is based on a bottom-up approach. We classify each node into one of three states: covered by a camera, has a camera, or needs a camera. A node needs a camera if its children are not covered. We use a post-order DFS strategy to ensure all children are addressed before deciding the current node's state. The optimal way to cover a subtree is to place a camera on nodes whose children are not covered.

Time Complexity: O(n), where n is the number of nodes in the tree, due to the DFS traversal of every node.
Space Complexity: O(h), where h is the height of the tree, due to the recursion stack space.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4struct TreeNode {
5    int val;
6    struct TreeNode 

    
    

Explanation

The code uses a post-order DFS traversal. It installs a camera at a node if either of its children need a camera (state 0). At the end, if the root itself needs a camera, we increment the count by one. Each node returns a state: 0 if it needs coverage, 1 if it has a camera, and 2 if it's covered.

Dynamic Programming with Memoization

This approach leverages dynamic programming through a recursive function with memoization to reduce redundant calculations. Each node can have three states represented in a tuple: it has a camera, it's covered but doesn't have a camera, or it's not covered. Using this state information, the solution calculates camera placements strategically.

Time Complexity: O(n), where n is the number of nodes.
Space Complexity: O(n), due to memoization storage.

1from functools import lru_cache
2
3class TreeNode:
4    def 

Video Solutions

Watch expert explanations and walkthroughs

a little secret for binary tree questions 🤫

NeetCode
1:00104,612 views

Asked By Companies

8 companies
F
Facebook
D
DE Shaw
A
Amazon
G
Google
A
Apple
T
TikTok
C
Cisco
P
PhonePe

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Unique Binary Search Trees IIMedium
Unique Binary Search TreesMedium
Binary Tree Maximum Path SumHard
Largest BST SubtreeMedium
More similar problems

Related Topics

Dynamic ProgrammingTreeDepth-First SearchBinary Tree

Problem Stats

Acceptance Rate46.9%
DifficultyHard
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Binary Tree Cameras asked in FAANG interviews?

Yes, Binary Tree Cameras is a classic hard tree problem that reflects concepts commonly tested in FAANG interviews. It evaluates understanding of DFS, greedy strategies, and tree-based dynamic programming.

What is the optimal approach for Binary Tree Cameras?

The optimal approach uses a post-order DFS with state tracking. Each node reports whether it has a camera, is covered, or is not covered. This allows the algorithm to greedily place cameras only when necessary, ensuring the minimum number of cameras.

Why is post-order traversal used in Binary Tree Cameras?

Post-order traversal processes child nodes before their parent, which allows the algorithm to determine whether the parent needs a camera based on the coverage status of its children. This bottom-up decision-making ensures optimal placement.

What data structure is best for solving Binary Tree Cameras?

A binary tree structure combined with recursion or a stack for DFS traversal works best. The solution relies on post-order traversal to evaluate children before deciding whether to place a camera at the current node.

*
left
;
7
struct
TreeNode
*
right
;
8
}
;
9
10
int
minCameras
;
11
12
int
dfs
(
struct
TreeNode
*
node
)
{
13
if
(
!
node
)
return
2
;
// if null, considered covered
14
15
int
left
=
dfs
(
node
->
left
)
;
16
int
right
=
dfs
(
node
->
right
)
;
17
18
if
(
left
==
0
||
right
==
0
)
{
19
minCameras
++
;
20
return
1
;
// install camera
21
}
22
return
(
left
==
1
||
right
==
1
)
?
2
:
0
;
23
}
24
25
int
minCameraCover
(
struct
TreeNode
*
root
)
{
26
minCameras
=
0
;
27
return
(
dfs
(
root
)
==
0
?
1
:
0
)
+
minCameras
;
28
}
__init__
(
self
,
val
=
0
,
left
=
None
,
right
=
None
)
:
5
self
.
val
=
val
6
self
.
left
=
left
7
self
.
right
=
right
8
9
class
Solution
:
10
def
minCameraCover
(
self
,
root
:
TreeNode
)
-
>
int
:
11
@lru_cache
(
None
)
12
def
dfs
(
node
)
:
13
if
not
node
:
14
return
(
0
,
0
,
float
(
'inf'
)
)
15
L
=
dfs
(
node
.
left
)
16
R
=
dfs
(
node
.
right
)
17
dp0
=
L
[
1
]
+
R
[
1
]
+
1
18
dp1
=
min
(
dp0
,
min
(
L
[
0
]
+
R
[
1
]
,
L
[
1
]
+
R
[
0
]
)
)
19
dp2
=
min
(
dp0
,
L
[
1
]
+
R
[
1
]
)
20
return
(
dp0
,
dp1
,
dp2
)
21
return
dfs
(
root
)
[
1
]

Explanation

This solution uses dynamic programming with memoization to avoid redundant recursive calls. The function `dfs` returns a tuple of three states for each node: the cost of having a camera, being covered without a camera, and not being covered.
The first element, `dp0`, calculates cost when a camera is placed at the current node. The second, `dp1`, calculates when the node is covered without having a camera, and the third, `dp2`, calculates when the node itself isn't covered.