Binary tree cameras
Standard · Write code · about 20 min
root is a binary tree. A camera placed on a node watches that node, its parent and its children.
Return the fewest cameras that watch every node.
Example 1
Input:
[[0,0,null,0,0]]
Output:
1
One camera on the left child watches itself, its parent and both children.
Example 2
Input:
[[0,0,null,0,null,0,null,null,0]]
Output:
2
Two cameras are needed along the left chain.
Constraints
- 0 ≤ number of nodes ≤ 1000
- Every node.val is 0
Learn the idea first: Tree DP, a visual lesson.
More Tree DP problems
- Fill the two numbers Warm-up
- House robber III Standard
- Longest univalue path Standard