Height of binary tree considering even level leaves only | 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 ; Driver Code ; Let us create binary tree shown in above diagram
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def heightOfTreeUtil ( root , isEven ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( not root . left and not root . right ) : NEW_LINE INDENT if ( isEven ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT left = heightOfTreeUtil ( root . left , not isEven ) NEW_LINE right = heightOfTreeUtil ( root . right , not isEven ) NEW_LINE if ( left == 0 and right == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + max ( left , right ) ) NEW_LINE DEDENT def heightOfTree ( root ) : NEW_LINE INDENT return heightOfTreeUtil ( root , False ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . left . right . left = newNode ( 6 ) NEW_LINE print ( " Height ▁ of ▁ tree ▁ is " , heightOfTree ( root ) ) NEW_LINE DEDENT