#C10020. Sum of Left Leaves in a Binary Tree
Sum of Left Leaves in a Binary Tree
Sum of Left Leaves in a Binary Tree
You are given a binary tree represented in level‐order as a comma-separated string, where the value null
represents a missing node. Your task is to compute the sum of all left leaves in the tree. A left leaf is a node that:
- Is a left child of its parent
- Does not have any children (i.e. it is a leaf node)
The input is provided in a single line and the output should be a single integer. Use any standard tree traversal technique (such as depth-first search) to solve the problem.
Note: In a binary tree, a node’s left child is considered a left leaf only if it is a leaf node. For example, consider the binary tree below:
3
├── 9
└── 20
├── 15
└── 7
Here, the left leaves are 9
(left child of 3) and 15
(left child of 20), so the sum is 9 + 15 = 24
.
inputFormat
The input consists of a single line containing comma-separated values that represent the nodes of the binary tree in level-order. The string null
represents a missing node.
Example: 3,9,20,null,null,15,7
outputFormat
Output a single integer which is the sum of all left leaves in the binary tree.
## sample3,9,20,null,null,15,7
24