Height of binary tree considering even level leaves only | Java 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
class GfG { static class Node { int data ; Node left ; Node right ; } static int heightOfTreeUtil ( Node root , boolean isEven ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) { if ( isEven == true ) 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 + Math . max ( left , right ) ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int heightOfTree ( Node root ) { return heightOfTreeUtil ( root , false ) ; } public static void main ( String [ ] args ) { 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 ) ; System . out . println ( " Height ▁ of ▁ tree ▁ is ▁ " + heightOfTree ( root ) ) ; } }