Question: ou are given a tree rooted at node 0 that consists of n nodes numbered from 0 to n - 1 . The tree is
ou are given a tree rooted at node that consists of n nodes numbered from to n The tree is represented by an array parent of size n where parenti is the parent of node i Since node is the root, parent
You are also given a string s of length n where si is the character assigned to node i
We make the following changes on the tree one time simultaneously for all nodes x from to n :
Find the closest node y to node x such that y is an ancestor of x and sx sy
If node y does not exist, do nothing.
Otherwise, remove the edge between x and its current parent and make node y the new parent of x by adding an edge between them.
Return an array answer of size n where answeri is the size of the subtree rooted at node i in the final tree.
A subtree of treeName is a tree consisting of a node in treeName and all of its descendants.
Note: Please do not copy the description during the contest to maintain the integrity of your submissions.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
