Find depth of the deepest odd level leaf node | A Binary tree node ; A recursive function to find depth of the deepest odd level leaf node ; Base Case ; If this node is leaf and its level is odd , return its level ; If not leaf , return the maximum value from left and right subtrees ; Main function which calculates the depth of deepest odd level leaf . This function mainly uses depthOfOddLeafUtil ( ) ; Driver program to test above function
class Node : 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 depthOfOddLeafUtil ( root , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT if root . left is None and root . right is None and level & 1 : NEW_LINE INDENT return level NEW_LINE DEDENT return ( max ( depthOfOddLeafUtil ( root . left , level + 1 ) , depthOfOddLeafUtil ( root . right , level + 1 ) ) ) NEW_LINE DEDENT def depthOfOddLeaf ( root ) : NEW_LINE INDENT level = 1 NEW_LINE depth = 0 NEW_LINE return depthOfOddLeafUtil ( root , level ) NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 6 ) NEW_LINE root . right . left . right = Node ( 7 ) NEW_LINE root . right . right . right = Node ( 8 ) NEW_LINE root . right . left . right . left = Node ( 9 ) NEW_LINE root . right . right . right . right = Node ( 10 ) NEW_LINE root . right . right . right . right . left = Node ( 11 ) NEW_LINE print " % d ▁ is ▁ the ▁ required ▁ depth " % ( depthOfOddLeaf ( root ) ) NEW_LINE