Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | ''Tree node ; ''Function to return new tree node ; ''Function to check if the tree is even-odd tree ; '' Stores nodes of each level ; '' Store the current level  of the binary tree ; '' Traverse until the  queue is empty ; '' Stores the number of nodes  present in the current level ; '' Insert left and right child  of node into the queue ; '' If the level is even ; '' If the nodes in this  level are in strictly  increasing order or not ; '' If the level is odd ; '' If the nodes in this  level are in strictly  decreasing order or not ; '' Increment the level count ; ''Driver code ; '' Construct a Binary Tree ; '' Check if the binary tree  is even-odd tree or not
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . left = None NEW_LINE self . right = None NEW_LINE self . val = data NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE return temp NEW_LINE DEDENT def checkEvenOddLevel ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE level = 0 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT l = [ ] NEW_LINE size = len ( q ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) ; NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) ; NEW_LINE DEDENT if ( level % 2 == 0 ) : NEW_LINE INDENT for i in range ( len ( l ) - 1 ) : NEW_LINE INDENT if ( l [ i + 1 ] > l [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT elif ( level % 2 == 1 ) : NEW_LINE INDENT for i in range ( len ( l ) - 1 ) : NEW_LINE INDENT if ( l [ i + 1 ] < l [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT level += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 2 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 11 ) NEW_LINE root . left . left . left = newNode ( 10 ) NEW_LINE root . left . left . right = newNode ( 5 ) NEW_LINE root . left . right . right = newNode ( 1 ) NEW_LINE if ( checkEvenOddLevel ( root ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT