Depth of the deepest odd level node in Binary Tree | Python3 program to find depth of the deepest odd level node Helper function that allocates a new node with the given data and None left and right poers . ; Constructor to create a new node ; Utility function which returns whether the current node is a leaf or not ; function to return the longest odd level depth if it exists otherwise 0 ; Base case return from here ; increment current level ; if curr_level is odd and its a leaf node ; A wrapper over deepestOddLevelDepth ( ) ; Driver Code ; 10 / \ 28 13 / \ 14 15 / \ 23 24 Let us create Binary Tree shown in above example
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 isleaf ( curr_node ) : NEW_LINE INDENT return ( curr_node . left == None and curr_node . right == None ) NEW_LINE DEDENT def deepestOddLevelDepthUtil ( curr_node , curr_level ) : NEW_LINE INDENT if ( curr_node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT curr_level += 1 NEW_LINE if ( curr_level % 2 != 0 and isleaf ( curr_node ) ) : NEW_LINE INDENT return curr_level NEW_LINE DEDENT return max ( deepestOddLevelDepthUtil ( curr_node . left , curr_level ) , deepestOddLevelDepthUtil ( curr_node . right , curr_level ) ) NEW_LINE DEDENT def deepestOddLevelDepth ( curr_node ) : NEW_LINE INDENT return deepestOddLevelDepthUtil ( curr_node , 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 28 ) NEW_LINE root . right = newNode ( 13 ) NEW_LINE root . right . left = newNode ( 14 ) NEW_LINE root . right . right = newNode ( 15 ) NEW_LINE root . right . right . left = newNode ( 23 ) NEW_LINE root . right . right . right = newNode ( 24 ) NEW_LINE print ( deepestOddLevelDepth ( root ) ) NEW_LINE DEDENT