Tree Sum Values
Published at25 Jul 2026
Created by M Ichsanul Fadhil
Given an array representing a binary tree (root at index 0, children at 2i+1 and 2i+2), compute the sum of all node values. This simulates summing all nodes in a binary tree stored as an array.
Simply iterate through the array and add all elements. The array uses the standard binary tree level-order representation.
Example Input & Output
Example 1
Input
[]
Output
0
Explanation
Empty
Example 2
Input
[10]
Output
10
Explanation
Single node
Example 3
Input
[1,2,3,4,5,6,7]
Output
28
Explanation
Complete tree
Example 4
Input
[1,2,3,4,5]
Output
15
Explanation
Tree as array, sum all values
Example 5
Input
[5,10,15]
Output
30
Explanation
Three nodes
Algorithm Flow

Recommendation Algorithm Flow for Tree Sum Values
Solution Approach
Best Answers
java
class Solution {
public int solution(int[] nums) {
int s=0;for(int n:nums)s+=n;return s;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
