Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ code please Leafy Trees Problem Description You are given a tree of size N rooted at node 1. The edges are given by the
C++ code please
Leafy Trees Problem Description You are given a tree of size N rooted at node 1. The edges are given by the vector P of size N-1 where ith element represents an edge between i+1 and P[i]. (1-based indexing) You need to answer Q queries on it. In each query, you are given L and R. You need to find such a node v such that all leaves with labels between L and R inclusive, lie in its subtree. The node V should also be at maximum height as possible. If there does not exist any leaf with label between L to R. return-1 for that query. Problem Constraints 15 N.Q. 32*105 15 LSRSN Input Format Inout consists of 2 arguments, first is the array A = P of size N-1 and second is the 2D array B of size Qx 2 representing queries in the same Here A and B are the names of the arguments in the function provided to you. Output Format Return a vector of Q integers, the answer to all the queries in order from 1 to Q. Example Input Input 1: P = [1, 2, 3, 4) queries - [1, 3] [3, 5] ] Input 2: A = [1, 1, 1, 1] queries = 1 [1, 5] [2, 3], Example Output Output 1 Output 2 Example Output Output 1: [-1, 5] Output 2: [1, 1, 4] Example Explanation Explanation 1: Query 1: There are no leafs in 1 to 3. Therefore, answer is -1. Query 2: There is only one leaf in 3 to 5, therefore the answer is 5. Explanation 2: Query 1: There are 4 leafs in 1 to 5, so we must take 1. Query 2: There are 2 leafs in 1 to 5, so again we must take 1 Query 3: There is only one leaf in 4 to 4, therefore the answer is 4. Editor Mode - Normal 1- vector int> Solution::solve (vector ints &A, vector vectorsint &B) { Submit Save ResetStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started