Given level order traversal of a Binary Tree , check if the Tree is a Min | C ++ program to check if a given tree is Binary Heap or not ; Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isMinHeap ( int level [ ] , int n ) { for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) return false ; if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) return false ; } } return true ; } int main ( ) { int level [ ] = { 10 , 15 , 14 , 25 , 30 } ; int n = sizeof ( level ) / sizeof ( level [ 0 ] ) ; if ( isMinHeap ( level , n ) ) cout << " True " ; else cout << " False " ; return 0 ; }