963. Minimum Vertex Cover on a Tree

HardTreesTree DPDFS

Given a tree with n nodes labeled 0..n-1 (rooted at 0) and an undirected edge list, return the size of the minimum vertex cover — the smallest set of nodes such that every edge has at least one endpoint in the set. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Integer — the minimum vertex cover size.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3],[1,4]]}
Output: 2
Explanation: Nodes 0 and 1 cover every edge.
Example 2
Input: {"n":1,"edges":[]}
Output: 0
Explanation: No edges.
Example 3
Input: {"n":2,"edges":[[0,1]]}
Output: 1
Explanation: One node covers the edge.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →