Please hel p me why this segment tree leaf node is not print ing the correct answer it should be ---->>>>>15 10 5 3 7 0 5 1 2 3 4 5 0 0 0 0 0
for array 1 2 3 4 5
mt segment tree leaf n ode should be 1 2 3 4 5 and then above laf node level it should be 3 7 5 and then 10 5 and then root should lbe 15 but its o/p is wrong
@kailash_01,
I think you are confused about the indexing, try making the tree on paper. Don’t think it bottom up, but rather top down, because not all leaves would be at the same level.
see the image for
6
1 2 3 4 5 6
my code is giving o/p
21 6 15 3 3 9 6 1 2 0 0 4 5 0 0 0 0 0 0 0 0 0 0 0
but correct o/p should be in the image that i solved in copy
I think you are confused because you are trying to build the tree bottom up, which is not the case with your recursive build, it is building top-down, that’s why the difference.
The image shown above is how actually your build function will make the tree.
thanku so much i understood now
and one last question is can we take segment tree array size 2n as you took in this pic actually in video bhaya told to take 4 n sze n=array size
@kailash_01,
No 2n can be too less in most cases, it may only work when n is a power of 2, so in general taking 4n is necessary.

