Dsa
Trees
Easy
Sum of Leaf Nodes
Write a function that computes the sum of the values of all leaf nodes in a binary tree. The input will be a level-order representation of the binary tree where none of the integers will be negative and a value of -1 indicates the absence of a node. The output should be a single integer representing the sum of all leaf nodes.
Input Format:
A single line of space-separated integers representing the level-order traversal of the tree.
Output Format:
A single integer value representing the sum of all leaf nodes.
Example:
Input:
1 2 3 -1 -1 -1 -1
Output:
5
Key concepts
treessumdepth-first
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now