Print all the leaf nodes of Binary Heap | C # implementation to print the leaf nodes of a Binary Heap ; Function to calculate height of the Binary heap with given the count of the nodes ; Function to find the leaf nodes of binary heap ; Calculate the height of the complete binary tree ; if the height if h - 1 , then there should not be any child nodes ; Function to print the leaf nodes ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int height ( int N ) { return ( int ) Math . Ceiling ( Math . Log ( N + 1 ) / Math . Log ( 2 ) ) - 1 ; } static void findLeafNodes ( int [ ] arr , int n ) { int h = height ( n ) ; List < int > arrlist = new List < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height ( i + 1 ) == h ) { arrlist . Add ( arr [ i ] ) ; } else if ( height ( i + 1 ) == h - 1 && n <= ( ( 2 * i ) + 1 ) ) { arrlist . Add ( arr [ i ] ) ; } else { break ; } } printLeafNodes ( arrlist ) ; } static void printLeafNodes ( List < int > arrlist ) { for ( int i = arrlist . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( arrlist [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; findLeafNodes ( arr , arr . Length ) ; } }