You are given an n-ary tree with a special property: If we burst a random node of the tree, this node along with its immediate parents up to the root vanishes. The tree has N nodes and nodes are numbered from 1 to N. The root is always at 1. Given a sequence of queries denoting the number of the node we start bursting, the problem is to find the number of subtrees that would be formed in the end according to the above property, for each query independently.
Input: Consider the following tree: 1 / | \ 2 3 4 / \ \ 5 6 7 / \ 8 9 q = 2 n = 1 n = 7 Output: 3 4 Explanation: In the first query after bursting node 1, there will be 3 subtrees formed rooted at 2, 3 and 4. In the second query after bursting node 7, nodes 4 and 1 also get burst, thus there will be 4 subtrees formed rooted at 8, 9, 2 and 3.
Since we are dealing with n-ary tree we can use a representation similar to that of a graph, and add the bidirectional edges in an array of lists. Now if we burst a node, we can say for sure that all its children will become separate subtrees. Moreover all the children of its parents and others ancestors till the root that burst, will also become separate subtrees. So in our final answer we want to exclude the current node and all its ancestors in the path till the root. Thus we can form the equation to solve as:
answer[node] = degree[node] + allChild[parent[node]] – countPath[node]
where allChild: number of node’s children + number of its
parent’s children + ..+ number of root’s children
parent: parent of a node in the tree
degree: number of children for a node
countPath: number of nodes from root to parent of node
We can fill all the above arrays using depth first search over the adjacency list.We can start from the root 1, assuming its parent is 0 and recur depth first to propagate its values to its children. Thus we can pre-process and fill the above arrays initially and return the equation’s value for each query accordingly.
Following is the C++ implementation of the above approach:
The time complexity of the above algorithm is O(E * lg(V)) where E id the number of edges and
V is the number of vertices.
- Iterative Preorder Traversal of an N-ary Tree
- Deepest right leaf node in a binary tree | Iterative approach
- Count Non-Leaf nodes in a Binary Tree
- Postorder traversal of Binary Tree without recursion and without stack
- Find next right node of a given key | Set 2
- Check mirror in n-ary tree
- LCA for n-ary Tree | Constant Query O(1)
- Diameter of an N-ary tree
- Locking and Unlocking of Resources arranged in the form of n-ary Tree
- Convert a given Binary Tree to Doubly Linked List | Set 3
- Check if a given Binary Tree is SumTree
- Level Order Tree Traversal
- If you are given two traversal sequences, can you construct the binary tree?
- Tree Traversals (Inorder, Preorder and Postorder)
- 2-3 Trees | (Search and Insert)
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.