Flattening of a tree(mini tutorial)

It was hard to find the exact code for this. As I’ve found it, I am sharing it :slight_smile:
This concept is used mainly when the problem involves subtree queries.
Suppose, we are given a tree and are asked to find sum of all the nodes of a subtree under node-‘x’.
And we are also given some point updates.
We can represent the tree as an array.
We do this by doing the Euler tour of the tree.
For a subtree under node ‘x’,all those elements/nodes lie in a range {l,r} of the array.
Now, we can use the segment tree concept to solve the problem :slight_smile:

Problem Link:-Subtree Queries | Practice Problems
My code link:-
Submission (25562623) for Subtree Queries | HackerEarth

7 Likes


seems like markdown didn’t work for your code link …

1 Like