#C9079. 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. Your task is to compute the sum of all its leaf nodes. A leaf node is defined as a node with no children. The binary tree is represented in level order traversal, where the keyword null
represents a missing node.
Note: The problem can be stated in mathematical terms as follows: Given a binary tree \(T\), compute \[ S = \sum_{v \in L(T)} v, \] where \(L(T)\) is the set of all leaf nodes in \(T\) and \(v\) is the value of the node.
Input is provided via standard input (stdin) and output must be printed to standard output (stdout).
inputFormat
The input consists of a single line containing space-separated tokens that represent the nodes of a binary tree in level order. The keyword null
denotes a missing/absent node. For example, a tree with root 1, left child 2, and right child 3 is represented as:
1 2 3
and a tree with a missing node might be represented as:
1 2 3 null 4
outputFormat
Output a single integer which is the sum of all the leaf nodes of the given tree.
## sample5
5