#C6611. Binary Tree Minimum Depth Sum
Binary Tree Minimum Depth Sum
Binary Tree Minimum Depth Sum
You are given a binary tree. The tree is represented in level order format, where missing nodes are denoted by null
. The task is to find the sum of the values of all nodes at the minimum depth of the tree. The minimum depth is defined as the shortest distance from the root node to any leaf node. Formally, if the minimum depth is \(d\), then you must calculate \(\sum_{node \in L_d} node.data\), where \(L_d\) is the set of leaf nodes at depth \(d\).
For example, consider the tree:
1 / \ 2 3
The leaf nodes at the minimum depth (level 2) are 2 and 3, and their sum is 5.
inputFormat
The input is a single line containing the level order traversal of the binary tree nodes separated by spaces. Use the string null
to denote missing nodes. For example, the input 1 2 3
represents a tree with root node 1 and two children 2 and 3.
outputFormat
Output a single integer which is the sum of the values of all nodes at the minimum depth (i.e. the level where the first leaf node is found).
## sample1 2 3
5