1201. Maximum Independent Set on Tree

MediumDynamic ProgrammingTree DP

Given a binary tree (level-order with null for missing nodes), an independent set is a set of nodes with no two connected by an edge (no parent-child pair). Return the maximum number of nodes in such a set. The input is JSON {tree}.

Input: JSON {tree}.

Output: Integer — the size of the maximum independent set.

Examples

Example 1
Input: {"tree":[1,2,3]}
Output: 2
Explanation: Choose the two children.
Example 2
Input: {"tree":[1,2,null,3]}
Output: 2
Explanation: Choose the root and grandchild.
Example 3
Input: {"tree":[1]}
Output: 1
Explanation: Single node.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →