VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-nodes-of-binary-tree-form-arithmetic-geometric-or-harmonic-progression/

⇱ Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression

Last Updated : 12 Jul, 2025

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
 

  • To check a sequence is in arithmetic progression or not, sort the sequence and check that the common difference between consecutive elements of the array is the same.
  • To check a sequence is in geometric progression or not, sort the sequence and check that the common ratio between the consecutive elements of the array is the same.
  • To check a sequence is in harmonic progression or not, find the reciprocal of every element and then sort the array and check that the common difference between the consecutive elements is the same.


Below is the implementation of the above approach: 
 


Output: 
Arithmetic Progression

 

Performance Analysis: 
 

  • Time Complexity: As in the above approach, there is a traversal of the nodes and sorting them which takes O(N*logN) time in worst case. Hence the Time Complexity will be O(N*logN).
  • Auxiliary Space Complexity: As in the above approach, There is extra space used to store the data of the nodes. Hence the auxiliary space complexity will be O(N).


 

Comment