#C12698. Sum of Left Leaves in a Binary Tree
Sum of Left Leaves in a Binary Tree
Sum of Left Leaves in a Binary Tree
Given a binary tree, a leaf is defined as a node with no children. A left leaf is a leaf that is the left child of its parent. Your task is to compute the sum of all left leaves in the provided binary tree.
The binary tree is given in a level-order sequence. Each token is separated by a space, and the token null
represents an absent (or null) node. For example, the binary tree shown below:
3 / \ 9 20 / \ 15 7
is represented by the input:
3 9 20 null null 15 7
The expected output for this tree is 24
.
inputFormat
The input is provided via standard input as a single line containing space-separated tokens. These tokens represent the nodes of a binary tree in level-order. The token null
is used to indicate a missing node. If the tree is empty, the input will be a single token null
.
outputFormat
The output is a single integer printed to standard output, representing the sum of all left leaves in the binary tree.
## sample3 9 20 null null 15 7
24