![]() |
VOOZH | about |
Given a binary tree, the task is to check whether the nodes in this tree form an arithmetic progression, geometric progression or harmonic progression.
Examples:
Input:
4
/ \
2 16
/ \ / \
1 8 64 32
Output: Geometric Progression
Explanation:
The nodes of the binary tree can be used
to form a Geometric Progression as follows -
{1, 2, 4, 8, 16, 32, 64}
Input:
15
/ \
5 10
/ \ \
25 35 20
Output: Arithmetic Progression
Explanation:
The nodes of the binary tree can be used
to form a Arithmetic Progression as follows -
{5, 10, 15, 20, 25, 35}
Approach: The idea is to traverse the Binary Tree using Level-order Traversal and store all the nodes in an array and then check that the array can be used to form an arithmetic, geometric or harmonic progression.
Below is the implementation of the above approach:
Arithmetic Progression
Performance Analysis: