You are given the root of a binary tree. You may install cameras on nodes, where each camera monitors:
Your task is to determine the minimum number of cameras required so that every node in the binary tree is monitored.
Return the minimum number of cameras needed.
Input: root = [0,0,null,0,0]
Output: 1
Explanation: A single camera placed on the second node can monitor all nodes in the tree.
Input: root = [0,0,null,0,null,0,null,null,0]
Output: 2
Explanation: At least two cameras are required to monitor every node in this skewed tree.
Input: root = [0]
Output: 1
Explanation: A single node needs one camera to be monitored.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 6 7
2
10 5 15 2 7 12 20
2