1-indexed array.
Then for node with index i, the left son has the index 2 * i, and the right one has 2 * i + 1.
Then go to the array from the end, now for node:
if the index of his (left or right) son goes beyond the bounds of the array, then he does not have (left or right) son.
If not, then you can find out the number of children of your son (we go through the array from the end). Result = number of children now son + number now son.
For instance:
[1, 2, 3, 4, 5, 6, 7] A is the result array. 1.A=[0, 0, 0, 0, 0, 0, 0],now(now is a index) = 7(1-indexed) since 7*2>7, a[7]=0 2.A=[0, 0, 0, 0, 0, 0, 0],now = 6,since 6*2>7, a[6]=0 3.A=[0, 0, 0, 0, 0, 0, 0],now = 5,since 5*2>7, a[5]=0 4.A=[0, 0, 0, 0, 0, 0, 0],now = 4,since 4*2>7, a[4]=0 5.A=[0, 0, 2, 0, 0, 0, 0],now = 3,since 3*2<7 and 3*2+1<7, a[3]=2+a[6]+a[7]=2 6.A=[0, 2, 2, 0, 0, 0, 0],now = 2,since 2*2<7 and 2*2+1<7, a[2]=2+a[4]+a[5]=2 7.A=[6, 2, 2, 0, 0, 0, 0],now = 1,since 1*2<7 and 1*2+1<7, a[1]=2+a[2]+a[3]=6