#C7928. Sum of Leaf Nodes in a Binary Tree
Sum of Leaf Nodes in a Binary Tree
Sum of Leaf Nodes in a Binary Tree
You are given a binary tree. A leaf node is a node with no children. Your task is to compute the sum of all leaf nodes in the tree.
Let \(T\) be a binary tree and \(L\) be the set of its leaves. The sum of leaf nodes is given by:
\[ S = \sum_{v \in L} v \]If the tree is empty, then \(S = 0\).
inputFormat
The input consists of a single line containing comma-separated tokens. Each token is either an integer or the string null
, representing the nodes of the binary tree in level order. The first token is the value of the root. For a missing child, the token null
is used.
Examples:
1,2,3,4,5,6,7
represents a tree where the leaves are 4, 5, 6, and 7.10,8,2,null,null,3,6
represents a tree with leaves 8, 3, and 6.null
represents an empty tree.
outputFormat
Output a single integer, which is the sum of all leaf nodes in the binary tree.
## sample1,2,3,4,5,6,7
22
</p>