#K51712. Binary Tree Vibration
Binary Tree Vibration
Binary Tree Vibration
You are given a binary tree in a level-order serialization format. The vibration of a binary tree is defined as the sum of the depths of all its nodes, where the depth of the root is \(0\), the depth of its children is \(1\), and so on.
Your task is to compute the vibration of the given binary tree.
Note: The input is provided as a single line containing space-separated tokens representing the level-order traversal of the tree. The keyword null
represents a missing node. For an empty tree, the input will be null
.
Example:
Input: 1 2 3 Output: 2</p>Explanation: The tree is: 1 /
2 3 The depths are 0 for node 1, and 1 each for nodes 2 and 3, so the vibration is 0+1+1 = 2.
inputFormat
The input consists of a single line of space-separated tokens representing the level-order traversal of the binary tree.
- If the tree is empty, the input is
null
. - Otherwise, each token is either an integer (representing a node's value) or the string
null
(representing a missing node).
outputFormat
Output a single integer representing the vibration of the binary tree, which is the sum of the depths of all nodes.
## samplenull
0