Check whether nodes of Binary Tree form Arithmetic , Geometric or Harmonic Progression | C # implementation to check that nodes of binary tree form AP / GP / HP ; Structure of the node of the binary tree ; Function to find the size of the Binary Tree ; Base Case ; Function to check if the permutation of the sequence form Arithmetic Progression ; If the sequence contains only one element ; Sorting the array ; Loop to check if the sequence have same common difference between its consecutive elements ; Function to check if the permutation of the sequence form Geometric progression ; Condition when the length of the sequence is 1 ; Sorting the array ; Loop to check if the the sequence have same common ratio in consecutive elements ; Function to check if the permutation of the sequence form Harmonic Progression ; Condition when length of sequence in 1 ; Loop to find the reciprocal of the sequence ; Sorting the array ; Loop to check if the common difference of the sequence is same ; Function to check if the nodes of the Binary tree forms AP / GP / HP ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize height ; Loop to traverse the tree using Level order Traversal ; Enqueue left child ; Enqueue right child ; Condition to check if the sequence form Arithmetic Progression ; Condition to check if the sequence form Geometric Progression ; Condition to check if the sequence form Geometric Progression ; Driver Code ; Constructed Binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7
using System ; using System . Collections . Generic ; public class GFG { public class Node { public int data ; public Node left , right ; public Node ( int data ) { this . data = data ; this . left = this . right = null ; } } ; static int size ( Node node ) { if ( node == null ) return 0 ; else return ( size ( node . left ) + 1 + size ( node . right ) ) ; } static bool checkIsAP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } static bool checkIsGP ( double [ ] arr , int n ) { if ( n == 1 ) return true ; Array . Sort ( arr ) ; double r = arr [ 1 ] / arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] / arr [ i - 1 ] != r ) return false ; } return true ; } static bool checkIsHP ( double [ ] arr , int n ) { if ( n == 1 ) { return true ; } double [ ] rec = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { rec [ i ] = ( ( 1 / arr [ i ] ) ) ; } Array . Sort ( rec ) ; double d = ( rec [ 1 ] ) - ( rec [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( rec [ i ] - rec [ i - 1 ] != d ) { return false ; } } return true ; } static void checktype ( Node root ) { int n = size ( root ) ; double [ ] arr = new double [ n ] ; int i = 0 ; if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 == false ) { Node node = q . Dequeue ( ) ; arr [ i ] = node . data ; i ++ ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; } int flag = 0 ; if ( checkIsAP ( arr , n ) ) { Console . WriteLine ( " Arithmetic ▁ Progression " ) ; flag = 1 ; } else if ( checkIsGP ( arr , n ) ) { Console . WriteLine ( " Geometric ▁ Progression " ) ; flag = 1 ; } else if ( checkIsHP ( arr , n ) ) { Console . WriteLine ( " Geometric ▁ Progression " ) ; flag = 1 ; } else if ( flag == 0 ) { Console . WriteLine ( " No " ) ; } } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . right = new Node ( 8 ) ; root . right . right . left = new Node ( 6 ) ; root . right . right . right = new Node ( 7 ) ; checktype ( root ) ; } }