Given a tree with N nodes value from 1 to N and (N – 1) Edges and a number K, the task is to remove the minimum number of nodes from the tree such that every subtree will have at most K nodes. Removing Nodes will remove the edges from that nodes to all the other connected nodes.
Input: N = 10, K = 3, Below is the graph:
Number of nodes removed: 2
Removed Nodes: 2 1
After removing the nodes 1 and 2, here, no subtree or tree has more than 3 nodes. Below is the resulting graph:
Input: N = 6, K = 3, Below is the graph:
Number of nodes removed: 1
Removed Nodes: 1
After removing the nodes 1, here, no subtree or tree has more than 3 nodes. Below is the resulting graph:
Approach: The idea is to observe that the number of nodes in the subtree of a node X is the sum of the number of nodes in the subtree of its children and the node itself. Below are the steps:
- Use Dynamic Programming and DFS to store the count of nodes in the subtree of each node easily.
- Now, to have no node with subtree having more than K nodes, the idea is to remove the node whenever it has more than K nodes in its subtree, and pass 0 to its parent.
- In the above step, we are having each node with nodes in its subtree not greater than K and minimizing the number of node removals.
Below is the implementation of the above approach:
Number of nodes removed: 1 Removed Nodes: 1
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Queries for the number of nodes having values less than V in the subtree of a Node
- Node having maximum number of nodes less than its value in its subtree
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Number of unique paths in tree such that every path has a value greater than K
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Subtree of all nodes in a tree using DFS
- Check if two nodes are in same subtree of the root node
- Count of nodes having odd divisors in the given subtree for Q queries
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Minimum number of edges required to be removed from an Undirected Graph to make it acyclic
- Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.