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:-https://www.hackerearth.com/problem/algorithm/subtree-queries/
My code link:-
https://www.hackerearth.com/submission/25562623/

6 Likes


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

1 Like