Height of binary tree considering even level leaves only | Program to find height of the tree considering only even level leaves . ; A binary tree node has data , pointer to left child and a pointer to right child ; Base Case ; left stores the result of left subtree , and right stores the result of right subtree ; If both left and right returns 0 , it means there is no valid path till leaf node ; Helper function that allocates a new node with the given data and NULL left and right pointers . ; Driver program to test above functions ; Let us create binary tree shown in above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left ; struct Node * right ; } ; int heightOfTreeUtil ( Node * root , bool isEven ) { if ( ! root ) return 0 ; if ( ! root -> left && ! root -> right ) { if ( isEven ) return 1 ; else return 0 ; } int left = heightOfTreeUtil ( root -> left , ! isEven ) ; int right = heightOfTreeUtil ( root -> right , ! isEven ) ; if ( left == 0 && right == 0 ) return 0 ; return ( 1 + max ( left , right ) ) ; } struct Node * newNode ( int data ) { struct Node * node = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = NULL ; node -> right = NULL ; return ( node ) ; } int heightOfTree ( Node * root ) { return heightOfTreeUtil ( root , false ) ; } int main ( ) { struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> left -> right -> left = newNode ( 6 ) ; cout << " Height ▁ of ▁ tree ▁ is ▁ " << heightOfTree ( root ) ; return 0 ; }