Reverse Level Order Traversal | A binary tree node ; Function to print reverse level order traversal ; Print nodes at a given level ; Compute the height of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node ; Compute the height of each subtree ; Use the larger one ; Let us create trees shown in above diagram
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 reverseLevelOrder ( root ) : NEW_LINE INDENT h = height ( root ) NEW_LINE for i in reversed ( range ( 1 , h + 1 ) ) : NEW_LINE INDENT printGivenLevel ( root , i ) NEW_LINE DEDENT DEDENT def printGivenLevel ( root , level ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT if level == 1 : NEW_LINE INDENT print root . data , NEW_LINE DEDENT elif level > 1 : NEW_LINE INDENT printGivenLevel ( root . left , level - 1 ) NEW_LINE printGivenLevel ( root . right , level - 1 ) NEW_LINE DEDENT DEDENT def height ( node ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT lheight = height ( node . left ) NEW_LINE rheight = height ( node . right ) NEW_LINE if lheight > rheight : NEW_LINE INDENT return lheight + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return rheight + 1 NEW_LINE DEDENT DEDENT 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 . left . right = Node ( 5 ) NEW_LINE print " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is " NEW_LINE reverseLevelOrder ( root ) NEW_LINE