964. Minimum Dominating Set on a Tree

HardTreesTree DPDFSGreedy

Given a tree with n nodes labeled 0..n-1 (rooted at 0) and an undirected edge list, return the size of the minimum dominating set — the smallest set of nodes such that every node is either in the set or adjacent to a node in the set. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Integer — the minimum dominating set size.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[1,4]]}
Output: 2
Explanation: Nodes 0 and 1 dominate the tree.
Example 2
Input: {"n":1,"edges":[]}
Output: 1
Explanation: The single node must be chosen.
Example 3
Input: {"n":3,"edges":[[0,1],[0,2]]}
Output: 1
Explanation: The center dominates both leaves.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →