Print the nodes at odd levels of a tree | Recursive Python3 program to print odd level nodes Utility method to create a node ; If empty tree ; If current node is of odd level ; Recur for children with isOdd switched . ; Driver code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def printOddNodes ( root , isOdd = True ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( isOdd ) : NEW_LINE INDENT print ( root . data , end = " ▁ " ) NEW_LINE DEDENT printOddNodes ( root . left , not isOdd ) NEW_LINE printOddNodes ( root . right , not isOdd ) 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 printOddNodes ( root ) NEW_LINE DEDENT